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