Supported Metacharacters

PCRE supports the metacharacters and metasequences listed in Table 1-26 through Table 1-30. For expanded definitions of each metacharacter, see Section 1.2.1.

Table 1-26. Character representations

Sequence

Meaning

a

Alert (bell), x07.



Backspace, x08, supported only in character class.

e

ESC character, x1B.

Newline, x0A.

Carriage return, x0D.

f

Form feed, x0C.

Horizontal tab, x09.

octal

Character specified by a three-digit octal code.

xhex

Character specified by a one- or two-digit hexadecimal code.

x{hex}

Character specified by any hexadecimal code.

cchar

Named control character.

Table 1-27. Character classes and class-like constructs

Class

Meaning

[...]

A single character listed or contained in a listed range.

[^...]

A single character not listed and not contained within a listed range.

[:class:]

POSIX-style character class valid only within a regex character class.

.

Any character except newline (unless single-line mode, /s).

C

One byte; however, this may corrupt a Unicode character stream.

w

Word character, [a-zA-z0-9_].

W

Non-word character, [^a-zA-z0-9_].

d

Digit character, [0-9].

D

Non-digit character, [^0-9].

s

Whitespace character, [ f ].

S

Non-whitespace character, [^ f ].

Table 1-28. Anchors and zero-width tests

Sequence

Meaning

^

Start of string, or after any newline if in multiline match mode, /m.

A

Start of search string, in all match modes.

$

End of search string or before a string-ending newline, or before any newline if in multiline match mode, /m.



End of string or before a string-ending newline, in any match mode.

z

End of string, in any match mode.

G

Beginning of current search.



Word boundary; position between a word character (w) and either a non-word character (W), the start of the string, or the end of the string.

B

Not-word-boundary.

(?=...)

Positive lookahead.

(?!...)

Negative lookahead.

(?<=...)

Positive lookbehind.

(?<!...)

Negative lookbehind.

Table 1-29. Comments and mode modifiers

Modifier/sequence

Mode character

Meaning

PCRE_CASELESS

i

Case-insensitive matching for characters with codepoints values less than 256.

PCRE_MULTILINE

m

^ and $ match next to embedded .

PCRE_DOTALL

s

Dot (.) matches newline.

PCRE_EXTENDED

x

Ignore whitespace and allow comments (#) in pattern.

PCRE_UNGREEDY

U

Reverse greediness of all quantifiers: * becomes non-greedy and *? becomes greedy.

PCRE_ANCHORED

Force match to start at the first position searched.

PCRE_DOLLAR_ENDONLY

Force $ to match at only the end of a string instead of before a string ending with a newline. Overridden by multiline mode.

PCRE_NO_AUTO_CAPTURE

Disable capturing function of parentheses.

PCRE_UTF8

Treat regular expression and subject strings as strings of multibyte UTF-8 characters.

(?mode)

Turn listed modes (imsxU) on for the rest of the subexpression.

(?-mode)

Turn listed modes (imsxU) off for the rest of the subexpression.

(?mode:...)

Turn listed modes (xsmi) on within parentheses.

(?-mode:...)

Turn listed modes (xsmi) off within parentheses.

Q

Quote all following regex metacharacters.

E

End a span started with Q.

(?#...)

Treat substring as a comment.

#...

Treat rest of line as a comment in PCRE_EXTENDED mode.

Table 1-30. Grouping, capturing, conditional, and control

Sequence

Meaning

(...)

Group subpattern and capture submatch into 1,2,...

(?P<name>...)

Group subpattern and capture submatch into named capture group, name.

n

Contains the results of the nth earlier submatch from a parentheses capture group or a named capture group.

(?:...)

Group subpattern, but do not capture submatch.

(?>...)

Disallow backtracking for text matched by subpattern.

...|...

Try subpatterns in alternation.

*

Match 0 or more times.

+

Match 1 or more times.

?

Match 1 or 0 times.

{n}

Match exactly n times.

{n,}

Match at least n times.

{x,y}

Match at least x times, but no more than y times.

*?

Match 0 or more times, but as few times as possible.

+?

Match 1 or more times, but as few times as possible.

??

Match 0 or 1 time, but as few times as possible.

{n,}?

Match at least n times, but as few times as possible.

{x,y}?

Match at least x times, no more than y times, and as few times as possible.

*+

Match 0 or more times, and never backtrack.

++

Match 1 or more times, and never backtrack.

?+

Match 0 or 1 times, and never backtrack.

{n}+

Match at least n times, and never backtrack.

{n,}+

Match at least n times, and never backtrack.

{x,y}+

Match at least x times, no more than y times, and never backtrack.

(?(condition)...|...)

Match with if-then-else pattern. The condition can be either the number of a capture group or a lookahead or lookbehind construct.

(?(condition)...)

Match with if-then pattern. The condition can be either the number of a capture group or a lookahead or lookbehind construct.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.154.18