PCREck

a multi-dialect regular expression editor

(? )
PCRE Options

Subject #-1

--
--

Subject #0

--
--
↓ view in a separate page

PCRE Characters

Ranges

. any single character
[abc] a single character of: a, b or c
[^abc] any single character except: a, b, or c
[a-z] any single character in the range a-z
[a-zA-Z] any single character in the range a-z or A-Z

Assertions

^ start of line
$ end of line
\b any word boundary character
\B any non-word boundary character
\A start of string
\z end of string

PCRE Controls

Quantifiers

(a|b) a or b
a? zero or one of a
a* zero or more of a
a+ one or more of a
a{3} exactly 3 of a
a{3,} 3 or more of a
a{3,6} between 3 and 6 of a

Subpattern modifiers

foo(?=bar) foo will match if it's followed by bar
foo(?!bar) foo will match if it's not followed by bar
(?<=foo)bar bar will match if it's preceded by foo
(?<!foo)bar bar will match if there isn't a foo behind it
(foo)?(?(1)(bar)) bar will be captured only if foo was

PCRE Character Classes

Generic character types

\s any whitespace character
\S any non-whitespace character
\d any digit
\D any non-digit
\w any word character (letter, number, underscore)
\W any non-word character

POSIX Classes

[:alnum:] letters and digits
[:alpha:] letters
[:ascii:] character codes 0 - 127
[:blank:] space or tab only
[:cntrl:] control characters
[:graph:] printing characters, excluding space
[:print:] printing characters, excluding space
[:punct:] printing characters, excluding letters and digits and spaces
[:space:] white space (not quite the same as \s)
[:xdigit:] hexadecimal digits

Usage examples

POSIX classes are supported only as sub-classes ([] define a class); to match hexadecimal digits along with the characters % and }:

[%{[:xdigit:]]

To negate a class, prefix the identifier with ^:

[%{[:^xdigit:]]

References

The PCRE cheat sheet was built from a number of sources:

  1. The PCRE manual was used as the primary reference
  2. Rubular; some of the ranges, quantifiers, and assertions were snooped from there as they were compatible with PCRE
  3. One PHP PCRE cheat sheet