truncate line before and after cariage return (\r) or new line (\n) with ellipses

﹥>﹥吖頭↗ 提交于 2019-12-13 21:24:53

问题


I have this paragraph (may be I have more than one paragraph)

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

on irb, I am assign it into 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.}

as per carriage return (\r) and new line (\n), I need only these details from above paragraph, any Idea How can I do that?

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....

very simple way - let's say I have a\nabc\r\nd\r\nab\neba and search keyword is b, I need only abcabeba


回答1:


I don't know how <em>\1</em> is relevant here, but probably, you want this, I suppose.

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

Note that the regex is in single-line mode.

Or, if you are doing this on Linux, and still need to deal with (not only \n but also) \r, then this might be safer:

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



回答2:


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


来源:https://stackoverflow.com/questions/16198602/truncate-line-before-and-after-cariage-return-r-or-new-line-n-with-ellipse

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!