An unescaped vertical bar(|) matches either the regular expression that precedes it or the regular expression that follows it. : Regexps Group « Development « Ruby






An unescaped vertical bar(|) matches either the regular expression that precedes it or the regular expression that follows it.


def show_regexp(a, re) 
    if a =~ re 
        "#{$`}<<#{$&}>>#{$'}"  
    else 
        "no match" 
    end 
end 
a = "this is a test" 
show_regexp(a, /d|e/) 
show_regexp(a, /al|lu/)
show_regexp(a, /is test|this is/) 

 








Related examples in the same category

1.use parentheses to group terms within a regular expression
2.\1 refers to the match of the first group
3.unescape HTML
4.Backslash Sequences in the Substitution