You can use parentheses to group terms within a regular
expression. Everything within the group is treated as a single regular
expression.
showRE('banana', /an*/)
|
� |
b<<an>>ana
|
showRE('banana', /(an)*/)
|
� |
<<>>banana
|
showRE('banana', /(an)+/)
|
� |
b<<anan>>a
|
a = 'red ball blue sky'
|
showRE(a, /blue|red/)
|
� |
<<red>> ball blue sky
|
showRE(a, /(blue|red) \w+/)
|
� |
<<red ball>> blue sky
|
showRE(a, /(red|blue) \w+/)
|
� |
<<red ball>> blue sky
|
showRE(a, /red|blue \w+/)
|
� |
<<red>> ball blue sky
|
showRE(a, /red (ball|angry) sky/)
|
� |
no match
|
a = 'the red angry sky'
|
showRE(a, /red (ball|angry) sky/)
|
� |
the <<red angry sky>>
|
Parentheses are also used to collect the results of pattern
matching. Ruby counts opening parentheses, and for each stores the result of
the partial match between it and the corresponding closing
parenthesis. You can use this partial match both within the remainder
of the pattern and in your Ruby program. Within the pattern, the
sequence
\1
refers to the match of the first group,
\2
the
second group, and so on. Outside the pattern, the special variables
$1
,
$2
, and so on, serve the same purpose.
"12:50am" =~ /(\d\d):(\d\d)(..)/
|
� |
0
|
"Hour is #$1, minute #$2"
|
� |
"Hour is 12, minute 50"
|
"12:50am" =~ /((\d\d):(\d\d))(..)/
|
� |
0
|
"Time is #$1"
|
� |
"Time is 12:50"
|
"Hour is #$2, minute #$3"
|
� |
"Hour is 12, minute 50"
|
"AM/PM is #$4"
|
� |
"AM/PM is am"
|
The ability to use part of the current match later in that match
allows you to look for various forms of repetition.
# match duplicated letter
|
showRE('He said "Hello"', /(\w)\1/)
|
� |
He said "He<<ll>>o"
|
# match duplicated substrings
|
showRE('Mississippi', /(\w+)\1/)
|
� |
M<<ississ>>ippi
|
You can also use back references to match delimiters.
showRE('He said "Hello"', /(["']).*?\1/)
|
� |
He said <<"Hello">>
|
showRE("He said 'Hello'", /(["']).*?\1/)
|
� |
He said <<'Hello'>>
|