Mega Code Archive

 
Categories / Ruby / Development
 

Use part of the current match later in that match allows you to look for various forms of repetition

def show_regexp(a, re)      if a =~ re          "#{$`}<<#{$&}>>#{$'}"      else          "no match"      end  end  # match duplicated letter  show_regexp('He said "Hello"', /(\w)\1/) # He said "He<<ll>>o"  # match duplicated substrings  show_regexp('Mississippi', /(\w+)\1/)  # M<<ississ>>ippi