Perl5 regular expressions
Here we summarize the syntax of Perl5.003 regular expressions, all of
which is supported by the Perl5 classes in this package. However, for
a definitive reference, you should consult the
perlre man page
that accompanies the Perl5 distribution and also the book
Programming Perl, 2nd Edition from O'Reilly & Associates.
We are working toward implementing the features added after Perl5.003
up to and including Perl 5.6. Please remember, we only guarantee
support for Perl5.003 expressions in version 2.0.
- Alternatives separated by |
- Quantified atoms
- Match at least n but not more than m times.
- Match at least n times.
- Match exactly n times.
- Match 0 or more times.
- Match 1 or more times.
- Match 0 or 1 times.
- regular expression within parentheses
- a . matches everything except \n
- a ^ is a null token matching the beginning of a string or line
(i.e., the position right after a newline or right before
the beginning of a string)
- a $ is a null token matching the end of a string or line
(i.e., the position right before a newline or right after
the end of a string)
- Character classes (e.g., [abcd]) and ranges (e.g. [a-z])
- Special backslashed characters work within a character
class (except for backreferences and boundaries).
- \b is backspace inside a character class
- Special backslashed characters
- null token matching a word boundary (\w on one side
and \W on the other)
- null token matching a boundary that isn't a
- Match only at beginning of string
- Match only at end of string (or before newline
at the end)
- carriage return
- digit [0-9]
- non-digit [^0-9]
- word character [0-9a-z_A-Z]
- a non-word character [^0-9a-z_A-Z]
- a whitespace character [ \t\n\r\f]
- a non-whitespace character [^ \t\n\r\f]
- hexadecimal representation of character
- matches the corresponding control character
- \nn or \nnn
- octal representation of character
unless a backreference. a
- \1, \2, \3, etc.
- match whatever the first, second,
third, etc. parenthesized group matched. This is called a
backreference. If there is no corresponding group, the
number is interpreted as an octal representation of a character.
- matches null character
- Any other backslashed character matches itself
- Expressions within parentheses are matched as subpattern groups
and saved for use by certain methods.
By default, a quantified subpattern is greedy .
In other words it matches as many times as possible without causing
the rest of the pattern not to match. To change the quantifiers
to match the minimum number of times possible, without
causing the rest of the pattern not to match, you may use
a "?" right after the quantifier.
- Match 0 or more times
- Match 1 or more times
- Match 0 or 1 time
- Match exactly n times
- Match at least n times
- Match at least n but not more than m times
Perl5 extended regular expressions are fully supported.
- An embedded comment causing text to be ignored.
- Groups things like "()" but doesn't cause the
group match to be saved.
A zero-width positive lookahead assertion. For
example, \w+(?=\s) matches a word followed by
whitespace, without including whitespace in the
A zero-width negative lookahead assertion. For
example foo(?!bar) matches any occurrence of
"foo" that isn't followed by "bar". Remember
that this is a zero-width assertion, which means
that a(?!b)d will match ad because a is followed
by a character that is not b (the d) and a d
follows the zero-width assertion.
- One or more embedded pattern-match modifiers.
i enables case insensitivity, m enables multiline
treatment of the input, s enables single line treatment
of the input, and x enables extended whitespace comments.
Copyright © 2001 Apache Software Foundation. All Rights Reserved.