I'm trying to use a named capture group inside a block in Ruby. $1
still works, but I'd like to reference it using the name I gave.
"foo /(bar)".gsub(/(?<my_word> \(.*?\) )/x) do |match| puts "$1 = #{$1} and $my_word = #{$my_word}" end
Expected:$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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With