21
我想在Ruby中使用块内的命名捕获组。 $1
仍然有效,但我想用我给的名字来引用它。在Ruby中使用命名的捕获组gsub块(正则表达式)
"foo /(bar)".gsub(/(?<my_word> \(.*?\))/x) do |match|
puts "$1 = #{$1} and $my_word = #{$my_word}"
end
预计:$1 = (bar) and $my_word = (bar)
非常好,谢谢。我以前从来没有见过类似这样的语法。 – Chris
你能给我一个很好的正则表达式教程链接吗?我知道关于'regex'的'zero'。 :( –
http://www.regular-expressions.info/tutorial.html – oldergod