Ruby中的动态正则表达式

8
我正在尝试找到一种方法,让我能够从字符串(来自数据库)动态创建一个正则表达式对象,然后使用它来过滤另一个字符串。这个例子是从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

What I want to happen

>> 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"
2个回答

12

你只缺少一个东西:

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

通常,如果您粘贴了“错误”的行的输出而不仅仅是输入,您可能会注意到ws没有被适当地转义。


完美,谢谢 - 我知道我一定在做某些微妙的错误。 - davidsmalley

0

选项1:

# Escape the slashes:
r = Regexp.new("[A-Za-z]+: Revision ...[\\w]+ committed by [A-Za-z\\s]+")
缺点:需要手动转义所有已知的转义字符。
选项2:
# Use slashes in constructor
r = Regexp.new(/[A-Za-z]+: Revision ...[\w]+ committed by [A-Za-z\s]+/)

缺点:无


对于选项2 - 构造函数的参数始终为字符串,因为正则表达式是从数据库中提取的,所以在这种情况下不起作用。 - davidsmalley

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接