Mega Code Archive

 
Categories / Ruby / Development
 

Use back references to match delimiters

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'>>