2013-04-24 53 views
0

我有这个款之后(可能是我有一个以上的段落)截断前行和cariage回报( R)或新行( n)的用省略号

=> "On the server side,\n\nmy EJB3 application uses Spring for configuring all sorts of things. So,\n\nmy EJBs all look up various ApplicationContexts and use them as a sort of\r\nwell, I was going to say poor man's JNDI, but the reality is that JNDI in the J2EE environment is really a poor man's Spring. :-)\n\nOn the GUI side,\n\nI use it instead of resource injection to get access to my EJBs.\n\nThat lets me test the GUI component with simple pojos.\n\nHope that helps." 

irb,我分配成x

x = %q{On the server side,\n\nmy EJB3 application uses Spring for configuring all sorts of things. So,\n\nmy EJBs all look up various ApplicationContexts and use them as a sort of\r\nwell, I was going to say poor man's JNDI, but the reality is that JNDI in the J2EE environment is really a poor man's Spring. :-)\n\nOn the GUI side,\n\nI use it instead of resource injection to get access to my EJBs. \n\nThat lets me test the GUI component with simple pojos.\n\nHope that helps.} 

按回车(\ r)和新行(\ n)的,我只需要这些从上面的段落细节,任何想法我怎么能这样做?

my EJB3 application uses Spring for configuring all sorts of things. So,...my EJBs all look up various ApplicationContexts and use them as a sort of...I use it instead of resource injection to get access to my EJBs.... 

很简单的办法 - 让我们说我有a\nabc\r\nd\r\nab\neba和搜索关键字为b,我只需要abcabeba

+0

@sawa - 比方说我的关键字是' EJB',所以我需要截断最近的\ r或\ n这个单词,例如 - 这里是字符串a \ nabc \ r \ nd \ r \ nab \ neba和search关键字是“b”,我只需要abcabeba – 2013-04-24 17:52:36

+0

@sawa也更新问题 – 2013-04-24 17:59:11

+0

@sawa - 是基于 – 2013-04-24 18:04:10

回答

1

我不知道该怎么<em>\1</em>与此有关,但可能,你要想这, 我想。

x.scan(/.*ejb.*/i).join("...") 

请注意,正则表达式处于单行模式。

或者,如果你在Linux上做这个,仍然需要处理(不仅\n也)\r,那么这可能是更安全:

x.scan(/[^\n\r]*ejb[^\n\r]*/i).join("...") 
+0

是的,这里有关,先替换(你昨天回答),之后我正在寻找 – 2013-04-24 18:26:54

+0

多线模式的意思?你有一些链接的例子吗?为我的知识 – 2013-04-24 18:29:36

+0

请谷歌它。在多线模式下,'.'匹配任何字符。否则,'.'匹配除尾行字符以外的任何字符。 – sawa 2013-04-24 18:30:09

0
s = eval("%q{a\nabc\r\nd\r\nab\neba}") 
p s.split("\n").select{|x| x.include? 'b'}.join('') 
#=>"abcabeba" 
+0

单个单词它工作正常,比方说s =“banana \ n \ rapple \ nmango \ rapple \ n \ norange \ rapple“,我正在搜索”苹果“ – 2013-04-24 18:26:07