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)

see Character representation

(K)

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

Character classes and related constructs

see Character classes and related constructs

  

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

see Almost any character: dot

  

Any character except newline: Dot (with /s: any character)

see Unicode combining-character sequence: \X

  

Unicode character with any combining characters: \X

see Exactly one byte

  

Forced matching of a single byte (use caution!): \C

see Character-class shorthands: \w, \d, \s, \W, \D, \S

(K)

Character-class shorthands: (2) \w \d \s \W \D \S

see Unicode properties, scripts, and blocks: \p{Prop}, \P{Prop}

(K)

Unicode properties, scripts, and blocks: (3) \p{Prop} \P{Prop}

Anchors and other zero-width assertions

see Start of line, start of string: ^, \A

  

Start of line or string: ^ \A

see End of line, end of string: $, \Z, \z

  

End of line or string: $ \z \Z

see Using ˹\G˼

  

End of the previous match: \G

see Word boundaries: \b, \B, \<, \>, ...

  

Word boundary: (4) \b \B

see Lookahead; lookbehind

  

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

Comments and mode modifiers

see Mode modifiers: (?Modifikator), e.g. (?i) and (?-i)

  

Mode modifiers: (6) (?mod-mod) Allowed modifiers: x s m i (see Regex modifiers)

see Scoped mode modifiers: (?Modifikator:...), e.g. (?i:...)

  

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

see Comments: (?#...) and #...

  

Comments: (?#...) #... (from ›#‹ to the next newline or the end of the regex)

Grouping, capturing text, conditionals, and Perl code in the regex

see Grouping and capturing parentheses: (...), \1, \2, ...

  

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

see Noncapturing grouping parentheses: (?:...)

  

Noncapturing grouping parentheses: (?:...)

see Atomic parentheses

  

Atomic groups: (?>...)

see Alternation

  

Alternation: |

see Conditional regular expressions

  

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

see Greedy quantifiers

  

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

see Lazy quantifiers

  

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

see The Perl code-pattern construct

  

Perl code patterns: (?{...})

see The dynamic regex construct

  

Dynamic regular expressions: (??{...})

Only in regex literals

see Variable interpolation

(K)

Variable interpolation: $name @name

see Convert the next character to lowercase or uppercase

(K)

Convert the next character to lowercase or uppercase: \l \u

see Convert a range to lowercase or uppercase

(K)

Convert a range to lowercase or uppercase: \L \U ... \E

see Interpret a range as a literal string

(K)

Interpret a range as a literal string: \Q ... \E

see Unicode character names

(K)

Unicode character names: \N{name} – optional; see 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 /x handles only ASCII whitespace. The modifier /m likewise affects only newlines, not the full range of Unicode line terminators.

The modifier /i also 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