def show_regexp(a, re) if a =~ re "#{$`}<<#{$&}>>#{$'}" else "no match" end end show_regexp('He said "Hello"', /(["']).*?\1/) # He said <<"Hello">> show_regexp("He said 'Hello'", /(["']).*?\1/) # He said <<'Hello'>>