我试图找到一种方法让我从字符串(取自数据库)动态创建一个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"
答案 0 :(得分:11)
你只缺少一件事:
>> 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
未正确转义< / p>
答案 1 :(得分: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]+/)
缺点:无