如何在ROR中使用正则表达式

时间:2010-06-26 12:53:03

标签: ruby-on-rails regex

您好我有以下巨大的字符串格式

示例格式:

p=" --0016367d537a47795e0489ecb3c7\nContent-Type: text/plain; charset=ISO-8859-1\n\nok this is tested here\n and again going to test it \n\n\nOn Sat, Jun 26, 2010 at 4:20 PM, kumar \n <example@gmail.com> wrote:\n\n>"

从上面的巨大字符串中我只需要以下内容。

"ok this is tested here \n and again going to test it" 

我已经实现了以下正则表达式,但没有使用我的常规exp如下

p.match(/(^\n\n*.*\n\n)/)

但格式是不会返回所需的输出.....可以任何人请帮忙

2 个答案:

答案 0 :(得分:2)

irb(main):008:0> p.split("\n\n")[1]
=> "ok this is tested here\n and again going to test it "

答案 1 :(得分:0)

您使用的\ n与退格键不匹配,因为这是一个关键的正则表达式字符。为了找到\作为匹配的一部分,您必须使用\\。

以下是我测试过的示例,以匹配您所需的群组:

\\n\\n([\s\S]+)\\n\\n\\n