![](/img/trans.png)
[英]ruby regex scan and gsub work differently with capture groups in blocks
[英]Using named capture groups inside Ruby gsub blocks (regex)
我正在尝试在Ruby中的块中使用命名捕获组。 $1
仍然有效,但我想用我给的名字来引用它。
"foo /(bar)".gsub(/(?<my_word> \(.*?\) )/x) do |match|
puts "$1 = #{$1} and $my_word = #{$my_word}"
end
预期: $1 = (bar) and $my_word = (bar)
你在找
"foo /(bar)".gsub(/(?<my_word> \(.*?\) )/x) do |match|
puts "$1 = #{$1} and $my_word = #{$~[:my_word]}"
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.