Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试在 Ruby 的块内使用命名的捕获组。$1仍然有效,但我想用我给的名字来引用它。
$1
"foo /(bar)".gsub(/(?<my_word> \(.*?\) )/x) do |match| puts "$1 = #{$1} and $my_word = #{$my_word}" end
预期的:$1 = (bar) and $my_word = (bar)
$1 = (bar) and $my_word = (bar)
你正在寻找
"foo /(bar)".gsub(/(?<my_word> \(.*?\) )/x) do |match| puts "$1 = #{$1} and $my_word = #{$~[:my_word]}" end