Let??™s consider a few examples:
/wmd/i: Matches WMD, wMD, WMd, wmd, and any other case variation of the string wmd.
/taxation/gi: Locates all occurrences of the word taxation. You might use the
global modifier to tally up the total number of occurrences, or use it in conjunction
with a replacement feature to replace all occurrences with some other string.
Metacharacters
Perl regular expressions also employ metacharacters to further filter their searches. A
metacharacter is simply an alphabetical character preceded by a backslash that symbolizes
special meaning. A list of useful metacharacters follows:
\A: Matches only at the beginning of the string.
\b: Matches a word boundary.
\B: Matches anything but a word boundary.
\d: Matches a digit character. This is the same as [0-9].
\D: Matches a nondigit character.
\s: Matches a whitespace character.
\S: Matches a nonwhitespace character.
[]: Encloses a character class.
(): Encloses a character grouping or defines a back reference.
$: Matches the end of a line.
^: Matches the beginning of a line.
.: Matches any character except for the newline.
\: Quotes the next metacharacter.
\w: Matches any string containing solely underscore and alphanumeric characters.
This is the same as [a-zA-Z0-9_].
\W: Matches a string, omitting the underscore and alphanumeric characters.
Let??™s consider a few examples. The first regular expression will match strings such
as pisa and lisa but not sand:
242 CHAPTER 9 ?– STRINGS AND REGULAR EXPRESSIONS
/sa\b/
The next returns the first case-insensitive occurrence of the word linux:
/\blinux\b/i
The opposite of the word boundary metacharacter is \B, matching on anything but
a word boundary.
Pages:
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320