These are numbered according to the order of the capture groups (as ordered by their left hand paren).
You can use them (as shown) in the right-hand-side of a search and replace, but they're also valid perl variables which you can make use of after a substitution or ordinary match:
Since regexp could in principle overwrite these global special variables, you should make use of them immediately after the regexp and/or save off their values into more appropriateley named vars. (e.g. if you call a subroutine, you don't know if that sub has done a regex and overwritten them).my $line = "furrfu"; $line =~ /(r+)/; print "$line contained ", length($1), " repeated 'r's\n";
Since parens are used for purposes other than grouping (e.g. to have alternating choices (foo|bar|baz), you can use a "non-capturing group", with (?:).
Search for 'captur' in perlre for more gory details.
In reply to Re: simple regexp question
by jbert
in thread simple regexp question
by KarthikK
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |