pcre Syntax
The
pcre
keyword accepts standard Perl-compatible regular
expression (PCRE) syntax. The following sections describe that syntax.
Tip | While this section describes the basic syntax you may use for PCRE, you may want to consult an online reference or book dedicated to Perl and PCRE for more advanced information. |
Metacharacters
Metacharacters are literal characters that have special meaning within regular expressions. When you use them within a regular expression, you must ��escape” them by preceding them with a backslash.
The following table describes the metacharacters you can use with PCRE and gives examples of each.
Metacharacter |
Description |
Example |
---|---|---|
|
Matches any character except newlines. If
|
|
|
Matches zero or more occurrences of a character or expression. |
|
|
Matches zero or one occurrence of a character or expression. |
|
|
Matches one or more occurrences of a character or expression. |
|
|
Groups expressions. |
|
|
Specifies a limit for the number of matches for a character or expression. If you want to set a lower and upper limit, separate the lower limit and upper limit with a comma. |
|
|
Allows you to define character classes, and matches any character or combination of characters described in the set. |
|
|
Matches content at the beginning of a string. Also used for negation, if used within a character class. |
|
|
Matches content at the end of a string. |
|
|
Indicates an OR expression. |
|
|
Allows you to use metacharacters as actual characters and is also used to specify a predefined character class. |
|
Character Classes
Character classes include alphabetic characters, numeric characters, alphanumeric characters, and white space characters. While you can create your own character classes within brackets, you can use the predefined classes as shortcuts for different types of character types. When used without additional qualifiers, a character class matches a single digit or character.
The following table describes and provides examples of the predefined character classes accepted by PCRE.
Character Class |
Description |
Character Class Definition |
---|---|---|
\d |
Matches a numeric character (“digit”). |
[0-9] |
\D |
Matches anything that is not an numeric character. |
[^0-9] |
\w |
Matches an alphanumeric character (“word”). |
[a-zA-Z0-9_] |
\W |
Matches anything that is not an alphanumeric character. |
[^a-zA-Z0-9_] |
\s |
Matches white space characters, including spaces, carriage returns, tabs, newlines, and form feeds. |
[ \r\t\n\f] |
\S |
Matches anything that is not a white space character. |
[^ \r\t\n\f] |