使用省略号回车符(\ r)或新行(\ n)前后的行

时间:2013-04-24 17:34:49

标签: ruby jruby

我有这一段(可能我有一个以上的段落)

=> "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

2 个答案:

答案 0 :(得分:1)

我不知道<em>\1</em>在这里是如何相关的,但我想,你可能想要这个。

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

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

或者,如果您在Linux上执行此操作,并且仍然需要处理(不仅\n而且还需要处理\r),那么这可能更安全:

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

答案 1 :(得分:0)

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