当用作文字正则表达式时,转义的 Ruby 正则表达式不会给出匹配项。为什么?
代码说明了一切:
teststring = "helloworld$"
string_from_user = "world$"
regexp = Regexp.escape(string_from_user) # assigns "world\\$"
p teststring =~ Regexp.new(regexp) # prints 0 => match found
p teststring =~ /regexp/ # prints nil => no match
第一个匹配在 Regexp.escape 文档。 但为什么第二个版本不匹配呢?
我很担心,因为我需要将此正则表达式传递给第三方 Ruby 代码。该字符串来自用户,所以我想转义它。然后,在某些情况下,我可能会向该用户的字符串添加额外的正则表达式符号。例如,我可能会传递 "^helloworld\\$"
,以便第三方代码匹配 "helloworld$othercontent"
等字符串。
我担心如果第三方代码使用 =~ /regexp/
而不是 =~ Regexp.new(regexp)
,我会遇到麻烦,因为会有如上面的代码所示,没有匹配项。
Code says it all:
teststring = "helloworld$"
string_from_user = "world$"
regexp = Regexp.escape(string_from_user) # assigns "world\\$"
p teststring =~ Regexp.new(regexp) # prints 0 => match found
p teststring =~ /regexp/ # prints nil => no match
That the first one matches is mentioned in the Regexp.escape docs.
But why doesn't the second version match?
I'm concerned because I need to pass this regexp to third party Ruby code. The string comes from the user, so I want to escape it. Then, in some situations, I might add additional regexp symbols to this user's string. For example, I might pass "^helloworld\\$"
so that third party code would match strings like "helloworld$othercontent"
.
I am worried that if the third party code uses =~ /regexp/
instead of =~ Regexp.new(regexp)
, I will be in trouble, because there will be no match as indicated by the code above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为
/regexp/
是与字符串"regexp"
匹配的正则表达式。也许您的意思是/#{regexp}/
?编辑:通过更全面地阅读您的问题,我认为您将一个字符串传递到第三方代码中,您知道该代码将从该字符串创建正则表达式。在这种情况下,你应该是安全的。如上所述,
/regexp/
不可能是他们正在做的事情,因为它只是错误。他们必须使用 Regexp.new() 或类似的东西。Because
/regexp/
is a regexp matching the string"regexp"
. Perhaps you meant/#{regexp}/
?Edit: I take it, from reading your question more fully, that you're passing a string into third party code that you know will be making a Regexp from that string. In which case, you should be safe. As noted above,
/regexp/
cannot possibly be what they're doing, because it's just wrong. They must be usingRegexp.new()
or something similar.