Regular Expressions
Perl’s greatest strength is in text and file manipulation, which is accomplished by using the regular expression (regex) library. Regexes, which are quite different from the wildcard-handling and filename-expansion capabilities of the shell (see Chapter 12, “Automating Tasks and Shell Scripting”), allow complicated pattern matching and replacement to be done efficiently and easily.
For example, the following line of code replaces every occurrence of the string bob or the string mary with fred in a line of text:
$string =~ s/bob|mary/fred/gi;
Without going into too many of the details, Table 37.7 explains what the preceding line says.
Table 37.7. Explanation of $string =~ s/bob|mary/fred/gi;
If you are interested in the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access