判断字符串是否与Ruby中的正则表达式匹配的最快方法是什么?
我的问题是,我必须通过一个庞大的字符串列表"egrep",找到哪些字符串与运行时给定的regexp匹配.I only care about whether the string matches the regexp, not where it matches, nor what the content of the matching groups is.我希望这个假设可以用来减少我的代码在匹配regexp上花费的时间.
我用
pattern = Regexp.new(ptx).freeze
我发现string =~ pattern
比string.match(pattern)
稍微快一点.
有没有其他技巧或捷径可以让这个测试更快?