我想找到一种方法让我从一个字符串(从数据库中取出)动态创建一个正则表达式对象,然后用它来过滤另一个字符串。这个例子是从git提交消息中提取数据,但理论上任何有效的正则表达式都可以作为字符串出现在数据库中。Ruby中的动态正则表达式
会发生什么
>> string = "[ALERT] Project: Revision ...123456 committed by Me <[email protected]>\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 <[email protected]>\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"
完美,感谢 - 我知道我必须做一些微妙的错误。 – davidsmalley 2009-08-01 08:13:57