use parentheses to group terms within a regular expression
# Everything within the group is treated as a single regular expression.
def show_regexp(a, re)
if a =~ re
"#{$`}<<#{$&}>>#{$'}"
else
"no match"
end
end
show_regexp('banana', /an*/) # b<<an>>ana
show_regexp('banana', /(an)*/) # <<>>banana
show_regexp('banana', /(an)+/) # b<<anan>>a
Related examples in the same category