拿这个代码片断这应该与它的URL替换href标记:Ruby 1.9.3正则表达式与gsub:错误或功能?
irb> s='<p><a href="http://localhost/activate/57f7e805827f" style="color:#F19300;font-weight:bold">Click here!</a></p>'
irb> s.gsub(/<a href="([^ '"]*)"([^>]*)?>([^<]*)<\/a>/, "#{$1}")
=> "<p></p>"
此正则表达式失败(网址未找到)。然后,我逃避了正则表达式的<
性格,它的工作原理:
irb> s.gsub(/<a href="([^ '"]*)"([^>]*)?>([^\<]*)<\/a>/, "#{$1}")
=> "<p>http://localhost/activate/57f7e805827f</p>"
1:根据的RubyMine的检查,这种逃避不应该是必要的。它是否正确?如果是这样,为什么>
的逃跑显然不需要?
2:然后在同一会话IRB,用相同的字符串,原正则表达式突然也工作:
irb> s.gsub(/<a href="([^ '"]*)"([^>]*)?>([^<]*)<\/a>/, "#{$1}")
=> "<p>http://localhost/activate/57f7e805827f</p>"
这是因为再次调用gsub
当$1
变量没有被清除?如果是这样,这是故意的行为还是这是一个Ruby正则表达式错误?
3:当我改变字符串,并重新执行相同的命令,$1
只会调用gsub
后更改两次变更后的字符串:
irb> s='<p><a href="http://localhost/activate/xxxxyyy" style="color:#F19300;font-weight:bold">Click here!</a></p>'
=> "<p><a href=\"http://localhost/activate/xxxxyyy\" style=\"color:#F19300;font-weight:bold\">Click here!</a></p>"
irb> s.gsub(/<a href="([^ '"]*)"([^>]*)?>([^\<]*)<\/a>/, "#{$1}")
=> "<p>http://localhost/activate/57f7e805827f</p>"
irb> s.gsub(/<a href="([^ '"]*)"([^>]*)?>([^\<]*)<\/a>/, "#{$1}")
=> "<p>http://localhost/activate/xxxxyyy</p>"
这是故意的吗?如果是这样,这背后的逻辑是什么?
4:作为替换字符,有些教程建议使用"#{$n}"
,其他建议使用'\n'
。使用反斜杠变体时,上述问题不会出现。为什么 - 两者有什么区别?
谢谢!
谢谢,这使得总体感。 – Jens