[英]Using named capture groups inside Ruby gsub blocks (regex)
I'm trying to use a named capture group inside a block in Ruby. 我正在尝试在Ruby中的块中使用命名捕获组。
$1
still works, but I'd like to reference it using the name I gave. $1
仍然有效,但我想用我给的名字来引用它。
"foo /(bar)".gsub(/(?<my_word> \(.*?\) )/x) do |match|
puts "$1 = #{$1} and $my_word = #{$my_word}"
end
Expected: $1 = (bar) and $my_word = (bar)
预期:
$1 = (bar) and $my_word = (bar)
You are looking for 你在找
"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.