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. | ||
|---|---|---|
(K) |
| |
Character classes and related constructs | ||
see Ordinary classes | Classes: | |
Any character except newline: dot (with the s modifier: truly any character) | ||
(u) | Unicode combining sequence: | |
(K) | Character-class shorthands: | |
(K) (u) | Unicode properties and scripts:3. | |
see Exactly one byte | Exactly one byte (dangerous!):4. | |
Anchors and other zero-width assertions | ||
Start of line or string: | ||
End of line or string:5. | ||
End of the previous match: | ||
see Word boundaries | Word boundary: | |
Lookaround:6. | ||
Comments and mode modifiers | ||
see Mode modifiers | Mode modifiers: | |
Scope for mode modifiers: | ||
see Comments | Comments: | |
Grouping and capturing text | ||
Capturing parentheses: | ||
Named parentheses: | ||
Noncapturing grouping parentheses: | ||
Atomic groups: | ||
see Alternation | Alternation: | | |
Recursion: | ||
Conditional regular expressions: | ||
Greedy quantifiers: | ||
see Lazy quantifiers | Lazy quantifiers: | |
Possessive quantifiers: | ||
(K) | Literal-text mode: | |
(K) – also usable in a character class | 1. .... 7. see notes | |
This table summarizes the characteristics of PHP’s regex dialect.
\brepresents backspace only inside character classes. Outside them,\bis 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.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.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.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,\Cstill matches exactly one byte. See the remarks under Exactly one byte suffices.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.Lookbehind generally accepts only subexpressions matching fixed-length text. Only top-level alternation permits branches of different lengths (see Lookahead; lookbehind).
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