Matching Regular Expressions



next up previous
Next: Extracting Matched String Up: Regular Expressions Previous: Regular Expressions

Matching Regular Expressions

Perl regular expressions look very much like those in vi.

.
Match any one character except a newline.
c*
Match zero or more instances of character c.
c+
Match one or more instances of character c.
c?
Match zero or one instance of character c.
[class]
Match any of the characters in character class class.
\w
Match an alphanumeric character (including "_")
\W
Match an non-alphanumeric character (including "_")
\b
Match a word boundary
\B
Match non-boundaries
\s
Match a whitespace character
\S
Match a non-whitespace character
\d
Match a numeric character
\D
Match a non-numeric character
^ Match the beginning of a line
$
Match the end of a line

Also, \n, \r, \f, \t and \NNN have their usual C-style interpretations.

The actual syntax for the pattern matching command is m/pattern/gio. The modifiers are g for ``global'' match, i for ``ignore case'', and o for ``only compile this regexp once''. With the m command, you can use any pair of non-alphanumeric characters to bound the expression. This especially useful when matching filenames that contain the ``/'' character. For example:

if (m!^/tmp_mnt!)
{ print "$_ is an automounted file system\n"; }

If the delimiter you choose is ``/'', then the leading m is optional.

Perl even has the ability to do multi-line pattern matching. Refer to the documentation on the $* variable for complete information.