XML technologies / Regular expressions / Perl / Perl’s regex dialect
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
The following table summarizes the regex dialect of Perl. Years ago, Perl was the only language supporting so many metacharacters, but other languages have since caught up and implemented many Perl features. These common metacharacters, available in several languages, were covered in the overview under Features and dialects. This chapter discusses Perl-specific details of some of them, with references in the following table.
Table: Overview of regular-expression elements in Perl.
Character shorthands (1) | ||
|---|---|---|
(K) |
| |
Character classes and related constructs | ||
| Classes: | |
| Any character except newline: Dot (with | |
| Unicode character with any combining characters: | |
see Exactly one byte |
| Forced matching of a single byte (use caution!): |
(K) | Character-class shorthands: (2) | |
see Unicode properties, scripts, and blocks: \p{Prop}, \P{Prop} | (K) | Unicode properties, scripts, and blocks: (3) |
Anchors and other zero-width assertions | ||
| Start of line or string: | |
| End of line or string: | |
see Using ˹\G˼ |
| End of the previous match: |
| Word boundary: (4) | |
| Lookaround: (5) | |
Comments and mode modifiers | ||
| Mode modifiers: (6) | |
see Scoped mode modifiers: (?Modifikator:...), e.g. (?i:...) |
| Scope for mode modifiers: |
| Comments: | |
Grouping, capturing text, conditionals, and Perl code in the regex | ||
| Capturing parentheses: | |
| Noncapturing grouping parentheses: | |
| Atomic groups: | |
see Alternation |
| Alternation: |
| Conditional regular expressions: | |
| Greedy quantifiers: | |
see Lazy quantifiers |
| Lazy quantifiers: |
| Perl code patterns: | |
| Dynamic regular expressions: | |
Only in regex literals | ||
(K) | Variable interpolation: | |
(K) | Convert the next character to lowercase or uppercase: | |
(K) | Convert a range to lowercase or uppercase: | |
(K) | Interpret a range as a literal string: | |
(K) | Unicode character names: | |
| ||
(K) - can also be used in a character class | (1)...(6) see text | |
A few notes on the table:
(1)
˹\b˼is a backspace only within character classes; otherwise, it is a word boundary.
Octal escapes may have two or three digits.
The
˹\xhex˼sequence accepts two-digit hexadecimal numbers (it also accepts one digit, but issues a warning if warnings are enabled). The˹\x{hex}˼sequence accepts hexadecimal numbers of any length.
(2)
˹\w˼,˹\d˼and˹\s˼fully support Unicode.
˹\s˼does not recognize the ASCII character Vertical Tab (see Character representation).
(3)
Perl supports Unicode version 4.1.0.
Unicode writing systems (Scripts) are supported. Unicode property and script names may optionally begin with
›Is‹(see Scripts). Unicode block names may begin with›In‹. If a script has the same name,›In‹is mandatory.The compound
˹\p{L&}˼is supported, as are˹\p{Any}˼,˹\p{All}˼,˹\p{Assigned}˼and˹\p{Unassigned}˼.Full Unicode property names such as
˹\p{Letter}˼are supported. Name components may be separated by spaces, underscores, or nothing (˹(\p{Lowercase_Letter}˼may also be written as˹\p{LowercaseLetter}˼or˹\p{Lowercase●Letter}˼). Nevertheless, I recommend using the long names from the table Basic Unicode subproperties.
˹\p{^...}˼is the same as˹\P{...}˼.
(4)
Word boundaries also work with Unicode characters.
(5)
Lookaround constructs may contain capturing parentheses.
Lookbehind supports only subexpressions matching fixed-length strings.
(6)
The modifier
/xhandles only ASCII whitespace. The modifier/mlikewise affects only newlines, not the full range of Unicode line terminators.
The modifier
/ialso works with Unicode characters.
Not all metacharacters are equal before the regex engine. Some of these regex metacharacters are not processed by the regex engine at all. Rather, they are string metacharacters interpreted by Perl before it passes them as literals to the regex engine.
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