Difference between revisions of "Text Pattern RegEx in Perl"
From Free Knowledge Base- The DUCK Project: information for everyone
(New page: if ($string =~ m/regex/) { print 'match'; } else { print 'no match'; } Performing a regex search-and-replace is just as easy: $string =~ s/regex/replacement/g; Perl has a host...) |
m |
||
Line 10: | Line 10: | ||
Perl has a host of special variables that get filled after every m// or | Perl has a host of special variables that get filled after every m// or | ||
− | s/// regex match. $1, $2, $3, etc. hold the backreferences. $+ holds the | + | s/// regex match. |
− | last (highest-numbered) backreference. $& (dollar ampersand) holds the | + | : $1, $2, $3, etc. hold the backreferences. |
− | entire regex match. | + | : $+ holds the last (highest-numbered) backreference. $& (dollar ampersand) holds the entire regex match. |
− | @- is an array of match-start indices into the string. $-[0] holds the | + | : @- is an array of match-start indices into the string. |
− | start of the entire regex match, $-[1] the start of the first | + | : $-[0] holds the start of the entire regex match, |
− | backreference, etc. Likewise, @+ holds match-end indices (ends, not | + | : $-[1] the start of the first backreference, etc. Likewise, |
− | lengths). | + | : @+ holds match-end indices (ends, not lengths). |
+ | |||
+ | : $' (dollar followed by an apostrophe or single quote) holds the part of the string after (to the right of) the regex match. | ||
+ | : $` (dollar backtick) holds the part of the string before (to the left of) the regex match. | ||
− | |||
− | |||
− | |||
Using these variables is not recommended in scripts when performance | Using these variables is not recommended in scripts when performance | ||
matters, as it causes Perl to slow down all regex matches in your entire | matters, as it causes Perl to slow down all regex matches in your entire |
Revision as of 20:07, 20 June 2007
if ($string =~ m/regex/) { print 'match'; } else { print 'no match'; }
Performing a regex search-and-replace is just as easy:
$string =~ s/regex/replacement/g;
Perl has a host of special variables that get filled after every m// or s/// regex match.
- $1, $2, $3, etc. hold the backreferences.
- $+ holds the last (highest-numbered) backreference. $& (dollar ampersand) holds the entire regex match.
- @- is an array of match-start indices into the string.
- $-[0] holds the start of the entire regex match,
- $-[1] the start of the first backreference, etc. Likewise,
- @+ holds match-end indices (ends, not lengths).
- $' (dollar followed by an apostrophe or single quote) holds the part of the string after (to the right of) the regex match.
- $` (dollar backtick) holds the part of the string before (to the left of) the regex match.
Using these variables is not recommended in scripts when performance matters, as it causes Perl to slow down all regex matches in your entire script.
while ($string =~ m/regex/g) { print "Found '$&'. Next attempt at character " . pos($string)+1 . "\n"; }