我试图找到一种方法让我从字符串(取自数据库)动态创建一个
regexp对象,然后使用它来过滤另一个字符串.这个例子是从git提交消息中提取数据,但理论上任何有效的正则表达式都可以作为字符串存在于数据库中.
怎么了
>> string = "[ALERT] Project: Revision ...123456 committed by Me <me@me.com>\n on 2009- 07-28 21:21:47\n\n Fixed typo\n" >> r = Regexp.new("[A-Za-z]+: Revision ...[\w]+ committed by [A-Za-z\s]+") >> string[r] => nil
我想要发生什么
>> string = "[ALERT] Project: Revision ...123456 committed by Me <me@me.com>\n on 2009- 07-28 21:21:47\n\n Fixed typo\n" >> string[/[A-Za-z]+: Revision ...[\w]+ committed by [A-Za-z\s]+/] => "Project: Revision 123456 committed by Me"
解决方法
你只缺少一件事:
>> Regexp.new "\w" => /w/ >> Regexp.new "\\w" => /\w/
反斜杠是字符串中的转义字符.如果你想要一个文字反斜杠,你必须加倍.
>> string = "[ALERT] Project: Revision ...123456 committed by Me <me@me.com>\n on 2009- 07-28 21:21:47\n\n Fixed typo\n" => "[ALERT] Project: Revision ...123456 committed by Me <me@me.com>\n on 2009- 07-28 21:21:47\n\n Fixed typo\n" >> r = Regexp.new("[A-Za-z]+: Revision ...[\\w]+ committed by [A-Za-z\\s]+") => /[A-Za-z]+: Revision ...[\w]+ committed by [A-Za-z\s]+/ >> string[r] => "Project: Revision ...123456 committed by Me "
通常,如果您粘贴了“破损”行的输出,而不仅仅是输入,您可能已经发现w和s没有正确转义