XML technologies / Regular expressions / PHP / The regex dialect of PHP

(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)

 

Table: Overview of regular-expression elements in PHP’s preg package.

Character shorthands 1.

see Shorthand metacharacters

(K)

\a [\b] \e \f \n \r \t \oktal \xhex \x{hex} \cZeichen

Character classes and related constructs

see Ordinary classes

Classes: [...] [^...] (may also contain POSIX style constructs such as [:alpha:]; see POSIX character-class bracket expression)

see Almost any character: dot

Any character except newline: dot (with the s modifier: truly any character)

see Unicode combining-character sequence

(u)

Unicode combining sequence: \X

see Character-class shorthands

(K)

Character-class shorthands: \w \d \s \W \D \S (8-bit characters only)2.

see Unicode properties, scripts, and blocks

(K) (u)

Unicode properties and scripts:3. \p{Prop} \P{Prop}

see Exactly one byte

Exactly one byte (dangerous!):4. \C

Anchors and other zero-width assertions

see Start of line, start of string

Start of line or string: ^ \A

see End of line, end of string

End of line or string:5. $ \z \Z

see Start of the next search or end of the previous match

End of the previous match: \G

see Word boundaries

Word boundary: \b \B (8-bit characters only)2.

see Lookahead; lookbehind

Lookaround:6. (?=...) (?!...) (?<=...) (?<!...)

Comments and mode modifiers

see Mode modifiers

Mode modifiers: (?mod-mod) Allowed modifiers: x7. s m i X U

see Scoped mode modifiers

Scope for mode modifiers: (?mod-mod:...)

see Comments

Comments: (?#...) (with the x modifier, also from ›#‹ until newline or the end of the regex)

Grouping and capturing text

see Grouping and capturing parentheses

Capturing parentheses: (...) \1 \2 ...

see Named subexpressions

Named parentheses: (?P<Name...>) (?P=Name)

see Noncapturing grouping parentheses

Noncapturing grouping parentheses: (?:...)

see Atomic parentheses

Atomic groups: (?>...)

see Alternation

Alternation: |

Recursion: (?R) (?Zahl) (?P>Name)

see Conditional regular expressions

Conditional regular expressions: (? if then|else) – »if« can be lookaround, (R) or (Zahl).

see Greedy quantifiers

Greedy quantifiers: * + ? {n} {n,} {x,y}

see Lazy quantifiers

Lazy quantifiers: *? +? ?? {n}? {n,}? {x,y}?

see Possessive quantifiers

Possessive quantifiers: *+ ++ ?+ {n}+ {n,}+ {x,y}+

see Literal-text mode

(K)

Literal-text mode: \Q . . . \E

(K) – also usable in a character class
(u) – only with the u modifier; see PHP-specific modifiers
This table also applies to PCRE, the regex implementation used by preg; see Bringing order to regex dialects.

1. .... 7. see notes

This table summarizes the characteristics of PHP’s regex dialect.

  1. \b represents backspace only inside character classes. Outside them, \b is the word-boundary metacharacter (see Word boundaries).

    Octal escapes are limited to two- or three-digit 8-bit values. The sequence ˹\0˼ matches a NUL byte.

    With ˹\xhex˼, one or two hexadecimal digits are allowed, while the notation ˹\x{hex}˼ allows any number. Values above \x{FF} are allowed only with the u modifier (see PHP-specific modifiers). Without the u modifier, a value \x{FF} causes an invalid-regex runtime error.

  2. Even in UTF-8 mode, with the u modifier, word boundaries and shorthand classes such as ˹\w˼ refer only to ASCII letters. To benefit fully from Unicode, use ˹\pL˼ (see Unicode properties, scripts, and blocks) instead of ˹\w˼, ˹\pN˼ instead of ˹\d˼ and ˹\pZ˼ instead of ˹\s˼ suffices.

  3. Unicode version 4.1.0 is supported.

    Unicode scripts are written without the prefix ›Is‹ or ›In‹; a bare ˹\p{Cyrillic}˼ suffices.

    One- and two-letter Unicode properties are supported, for example ˹\p{Lu}˼, ˹\p{L}˼ and ˹\pL˼. Long names such as ˹\p{Letter}˼ cannot be used.

    Both the compound subproperty ˹\p{L&}˼ and the property ˹\p{Any}˼, which matches any character, are supported.

  4. Normally, the regex engine of the preg package operates on bytes. Then ˹\C˼ is equivalent to ˹(?s:.)˼, a dot with the s modifier. The u modifier switches the engine into UTF-8 mode, where a character can consist of up to 6 bytes. Even then, \C still matches exactly one byte. See the remarks under Exactly one byte suffices.

  5. The metacharacters ˹\z˼ and ˹\Z˼ both match the end of the subject text; ˹\Z˼ also matches before a final newline. The interpretation of ˹$˼ depends on the modifiers m and D (see Pattern modifiers), as follows. Without either modifier, ˹$˼ behaves like ˹\Z˼, matching the string end or the preceding newline. With the m modifier enabled, it also matches before any newline within the string. With the D modifier enabled, ˹$˼ behaves like ˹\z˼, matching only the string end. When both modifiers are enabled, D is ignored.

  6. Lookbehind generally accepts only subexpressions matching fixed-length text. Only top-level alternation permits branches of different lengths (see Lookahead; lookbehind).

  7. With the x modifier, free-spacing mode, only ASCII whitespace is allowed; other Unicode whitespace is not permitted.

  

 

 

 

German edition copyright © 2008 by O’Reilly Verlag GmbH & Co. KG
You may print the online version for private use.
Otherwise, this chapter from "Reguläre Ausdrücke" is subject to the same terms as the bound edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, storage and processing in electronic systems.

O’Reilly Verlag GmbH & Co. KG, Balthasarstr. 81, 50670 Köln

back to top