2013-04-24 55 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