在回车符(\ r)或换行符(\ n)之前和之后用省略号截断 [英] truncate line before and after cariage return (\r) or new line (\n) with ellipses

查看:143
本文介绍了在回车符(\ r)或换行符(\ n)之前和之后用省略号截断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个段落(也许我有多个段落)

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

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),我只需要以上段落中的这些详细信息,任何想法我该怎么做?

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

非常简单的方法-假设我有a\nabc\r\nd\r\nab\neba并且搜索关键字是b,我只需要abcabeba

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

推荐答案

我不知道<em>\1</em>在这里有什么意义,但是,我想,可能您想要这个.

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.

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

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("...")

这篇关于在回车符(\ r)或换行符(\ n)之前和之后用省略号截断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆