Perl - Pattern Matching and Substitution
In a Nutshell - CIW Course Section 2, Part B2, Chapter 2
Substitution Operators
We have seen how m/pattern/ can be used to match text. s/pattern/replacement/ can be used to replace the matched text with new text. The substitution operator, s///, has a number of modifuers which control the way it functions. These modifiers are place after the operator thus: s/pattern/replacement/[egix].
| Substitution Modifiers | |
|---|---|
| Modifier | Description |
| e | Evaluates the right side as an expression |
| g | Matches globally to find all occurrences |
| i | Performs case-insensitive pattern matching |
| x | Uses extended regular expressions |
I haven't worked out exactly what the 'e' and the 'x' modifiers do. The 'i' modifier is fairly self-explanatory. I did a little test with the 'g' modifier to ensure that it did what I assumed it did:
$myStr = "The brown fox accompanied the red fox";
$myStr =~ s/fox/cat/;
print("$myStr/n");
Output: The brown cat accompanied the red fox
$myStr = "The brown fox accompanied the red fox";
$myStr =~ s/fox/cat/g;
print("$myStr/n");
Output: The brown cat accompanied the red cat
Without the 'g' modifier it replaces only the first match, with the modifier it replaces all matches.
Back References
Back references provides a way to remember individual search results from within a regular expression. By placing parts of the regular expression within parentheses the result from that expression will be assigned to a special variable for later reference. The variables $1, $2, $3, etc are used for each occurrence of a parenthesised section of the regular expression.
$myStr = "The brown fox accompanied the red fox";
if ($myStr =~ m/(\S+)\s+(\S+)\s+(\S+)/)
{
print("$1\n");
print("$2\n");
print("$3\n");
}
This example will output the first three words from the supplied string. The regular expression is matching non-white space blocks and white space blocks alternatively, with the non-white space blocks parenthesised and therefore assigned to the special variables.
