Ruby:我可以避免在正则表达式中进行大量转义吗?
Ruby: can I avoid lots of escaping in a regex?
假设我想匹配像这样的 CVS 评论:
// $Source$
我的正则表达式目前看起来像这样:
if ( /^\/\/\s*$Source$/ =~ line)
哪个有效,但我想知道 - 是否有更漂亮的方式来编写它?
使用%r
语法:
if ( %r{//\s*$Source$} =~ line)
^^ I'm not sure whether ruby would allow unescaped `$` here or not
假设我想匹配像这样的 CVS 评论:
// $Source$
我的正则表达式目前看起来像这样:
if ( /^\/\/\s*$Source$/ =~ line)
哪个有效,但我想知道 - 是否有更漂亮的方式来编写它?
使用%r
语法:
if ( %r{//\s*$Source$} =~ line)
^^ I'm not sure whether ruby would allow unescaped `$` here or not