Parentheses can be used as "memory" for relating different parts of a match or for relating substitution to match
|
If a part of a regular expression is enclosed in parentheses, the MATCHED value is stored in temporary variables \1 \2 .. for first,second .. set of parentheses
-
/Geoffrey(.*)Fox/ when matched to Geoffrey Charles Fox stores
-
\1 = ' Charles ' which can be transferred to substitution string which could be
-
/Geoffrey \(\1\) Fox/ for result Geoffrey ( Charles ) Fox
-
Note ONLY use \1 \2 etc. in pattern. Use $1 $2 outside pattern
|
Parentheses can also be used to clarify meaning of a regular expression by defining precedence of a set of operations and so distinguish for instance
-
/(a|b)*/ from /a|(b*)/
-
There is a definite convention for precendence but as usual I recommend using parantheses and in Perl5(later) we will see how to distinguish use of parantheses for either clarificatiuon or defining matched groups.
|