home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    

Book HomeJava and XSLTSearch this book

4.6. Regular Expressions

Regular expressions are used several ways in Perl. They're used in conditionals to determine whether a string matches a particular pattern. They're also used to find patterns in strings and replace the match with something else.

The ordinary pattern match operator looks like /pattern/. It matches against the $_ variable by default. If the pattern is found in the string, the operator returns true (1); if there is no match, a false value ("") is returned.

The substitution operator looks like s/pattern/replace/. This operator searches $_ by default. If it finds the specified pattern, it is replaced with the string in replace. If pattern is not matched, nothing happens.

You may specify a variable other than $_ with the =~ binding operator (or the negated !~ binding operator, which returns true if the pattern is not matched). For example:

$text =~ /sampo/;

4.6.1. Pattern-Matching Operators

The following list defines Perl's pattern-matching operators. Some of the operators have alternative "quoting" schemes and have a set of modifiers that can be placed directly after the operators to affect the match operation in some way.

Searches a string for a pattern match. Modifiers are:




Match globally, i.e., find all occurrences.


Do case-insensitive pattern matching.


Treat string as multiple lines.


Compile pattern only once.


Treat string as single line.


Use extended regular expressions.

If / is the delimiter, then the initial m is optional. With m, you can use any pair of non-alphanumeric, non-whitespace characters as delimiters.

This operator is just like the m/pattern/ search, except it matches only once.

Creates a precompiled regular expression from pattern, which can be passed around in variables and interpolated into other regular expressions. The modifiers are the same as those for m// above.

Searches a string for pattern and replaces any match with the replacement text. Returns the number of substitutions made, which can be more than one with the /g modifier. Otherwise, it returns false (0). If no string is specified via the =~ or !~ operator, the $_ variable is searched and modified. Modifiers are:




Evaluate the right side as an expression.


Replace globally, i.e., all occurrences.


Continue search after g failed. No longer supported for s/// as of Perl 5.8.


Do case-insensitive pattern matching.


Treat string as multiple lines.


Compile pattern only once.


Treat string as single line.


Use extended regular expressions.

Any non-alphanumeric, non-whitespace delimiter may replace the slashes. If single quotes are used, no interpretation is done on the replacement string (the /e modifier overrides this, however).

This operator scans a string character by character and replaces all occurrences of the characters found in pattern1 with the corresponding character in pattern2. It returns the number of characters replaced or deleted. If no string is specified via the =~ or !~ operator, the $_ string is translated. Modifiers are:




Complement pattern1.


Delete found but unreplaced characters.


Squash duplicate replaced characters.

4.6.2. Regular Expression Syntax

The simplest kind of regular expression is a literal string. More complicated patterns involve the use of metacharacters to describe all the different choices and variations that you want to build into a pattern. Metacharacters don't match themselves, but describe something else. The metacharacters are:




Escapes the character(s) immediately following it


Matches any single character except a newline (unless /s is used)


Matches at the beginning of the string (or line, if /m is used)


Matches at the end of the string (or line, if /m is used)


Matches the preceding element 0 or more times


Matches the preceding element 1 or more times


Matches the preceding element 0 or 1 times


Specifies a range of occurrences for the element preceding it


Matches any one of the class of characters contained within the brackets


Groups regular expressions


Matches either the expression preceding or following it

The . (single dot) is a wildcard character. When used in a regular expression, it can match any single character. The exception is the newline character (\n), except when you use the /s modifier on the pattern match operator. This modifier treats the string to be matched against as a single "long" string with embedded newlines.

The ^ and $ metacharacters are used as anchors in a regular expression. The ^ matches the beginning of a line. This character should appear only at the beginning of an expression to match the beginning of the line. The exception to this is when the /m (multiline) modifier is used, in which case it will match at the beginning of the string and after every newline (except the last, if there is one). Otherwise, ^ will match itself, unescaped, anywhere in a pattern, except if it is the first character in a bracketed character class, in which case it negates the class.

Similarly, $ will match the end of a line (just before a newline character) only if it is at the end of a pattern, unless /m is used, in which case it matches just before every newline and at the end of a string. You need to escape $ to match a literal dollar sign in all cases, because if $ isn't at the end of a pattern (or placed right before a ) or ]), Perl will attempt to do variable interpretation. The same holds true for the @ sign, which Perl will interpret as an array variable start unless it is backslashed.

The *, +, and ? metacharacters are called quantifiers. They specify the number of times to match something. They act on the element immediately preceding them, which could be a single character (including the .), a grouped expression in parentheses, or a character class. The {...} construct is a generalized modifier. You can put two numbers separated by a comma within the braces to specify minimum and maximum numbers that the preceding element can match.

Parentheses are used to group characters or expressions. They also have the side effect of remembering what they matched so you can recall and reuse patterns with a special group of variables.

The | is the alternation operator in regular expressions. It matches either what's on its left side or right side. It does not affect only single characters. For example:


looks for any of the four words. You should use parentheses to provide boundaries for alternation:


This will match either "Andy" or "Andrew".

4.6.4. Character Classes

The [...] construct is used to list a set of characters (a character class) of which one will match. Brackets are often used when capitalization is uncertain in a match:


A dash (-) may be used to indicate a range of characters in a character class:

/[a-zA-Z]/;  # Match any single letter
/[0-9]/;     # Match any single digit

To put a literal dash in the list you must use a backslash before it (\-).

By placing a ^ as the first element in the brackets, you create a negated character class, i.e., it matches any character not in the list. For example:

/[^A-Z]/;    # Matches any character other than an uppercase letter

Some common character classes have their own predefined escape sequences for your programming convenience:




A digit, same as [0-9]


A nondigit, same as [^0-9]


A word character (alphanumeric), same as [a-zA-Z_0-9]


A non-word character, [^a-zA-Z_0-9]


A whitespace character, same as [ \t\n\r\f]


A non-whitespace character, [^ \t\n\r\f]


Match a character (byte)


Match P-named (Unicode) property


Match non-P


Match extended unicode sequence

While Perl implements lc() and uc( ), which you can use for testing the proper case of words or characters, you can do the same with escape sequences:




Lowercase until next character


Uppercase until next character


Lowercase until \E


Uppercase until \E


Disable pattern metacharacters until \E


End case modification

These elements match any single element in (or not in) their class. A \w matches only one character of a word. Using a modifier, you can match a whole word, for example, with \w+. The abbreviated classes may also be used within brackets as elements of other character classes.

4.6.8. Extended Regular Expressions

Perl defines an extended syntax for regular expressions. The syntax is a pair of parentheses with a question mark as the first thing within the parentheses. The character after the question mark gives the function of the extension. The extensions are:

A comment. The text is ignored.

This groups things like (...) but doesn't make backreferences.

A zero-width positive lookahead assertion. For example, /\w+(?=\t)/ matches a word followed by a tab, without including the tab in $&.

A zero-width negative lookahead assertion. For example, /foo(?!bar)/ matches any occurrence of foo that isn't followed by bar.

A zero-width positive lookbehind assertion. For example, /(?<bad)boy/ matches the word boy that follows bad, without including bad in $&. This works only for fixed-width lookbehind.

An experimental regular expression feature to evaluate any embedded Perl code. This evaluation always succeeds, and code is not interpolated.

A zero-width negative lookbehind assertion. For example, /(?<!=bad)boy/ matches any occurrence of boy that doesn't follow bad. This works only for fixed-width lookbehind.

Matches the substring that the standalone pattern would match if anchored at the given position.

Matches a pattern determined by a condition. condition should be either an integer, which is true if the pair of parentheses corresponding to the integer has matched, or a lookahead, lookbehind, or evaluate, zero-width assertion. no-pattern will be used to match if the condition was not meant, but it is also optional.

One or more embedded pattern-match modifiers. Modifiers are switched off if they follow a - (dash). The modifiers are defined as follows:




Do case-insensitive pattern matching.


Treat string as multiple lines.


Treat string as single line.


Use extended regular expressions.

Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.