Introducing UNIX and Linux |
PerlOverview |
Regular expressionsRegular expressions have been used extensively in the previous chapters, and it will come as no surprise that Perl also uses them. The difference between Perl's approach to regular expressions, and that of other utilities, is that they are central to the use of Awk, Sed and Grep. In Perl, the use of regular expressions is just one of many possibilities available to the programmer. Typical use of regular expressions in Perl involves the comparison or editing of strings. The operator used to manipulate regular expressions is
The simplest use of patterns is to match strings. The expression string evaluates to true if the string is matched by the pattern, and string evaluates to true if the string is not matched by the pattern. Worked exampleWrite a Perl fragment to take the variable if ($word !~ /^'.*'$/) { $word = "'" . $word . "'"; } |
Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck