Text Pattern RegEx in Perl

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";
}

 

Last modified on 30 January 2014, at 16:59