You are looking for
"foo /(bar)".gsub(/(?<my_word> \(.*?\) )/x) do |match| puts "$1 = #{$1} and $my_word = #{$~[:my_word]}"end