Perl Compatible Regular Expressions
- Last Updated: August 5, 2025
- 1 minute read
- LoadMaster
- LoadMaster GA
- Documentation
Perl Compatible Regular Expressions (PCRE) implements regular expression pattern matching. It uses the same syntax and semantics as Perl 5. For further information regarding PCRE, refer to www.PCRE.org
When using special characters in PCRE, it is best practice to use the character’s ASCII or HTML equivalent rather than the actual character. For example, to match the percentage symbol, instead of writing /%/, for the HTML version, use /%/, and for the ASCII version use /\x25/.
To ensure that an expression is treated as a PCRE, the expression must be enclosed by the forward-slash character (/) or it will be treated as a Shell Regular Expression. For example, a PCRE expression would look like this: /^[Tt]est$/.
|
Character |
Meaning |
|---|---|
| . |
Matches any character but a line-break |
|
\d |
Matches any numeric digit |
|
\w |
Matches any alpha character |
| [] |
Matches a set of characters |
|
? |
Optionally matches the previous expression |
|
* |
Matches the previous expression zero or more times |
|
+ |
Matches the previous expression one or more times |
|
{x} |
Matches the previous expression x times |
|
{x, y} |
Matches the previous expression x to y times |
|
^ |
Matches the beginning of the string/line |
|
$ |
Matches the end of the string/line |
|
(x) |
Allows grouping of expressions |
|
a|b |
Alternative expressions, matches a OR b |
PCRE Examples
Some PCRE examples are below:
- ^/$ matches / and / only
- ^.*test.*$ matches the whole line of any line where test is mentioned
- [A-F0-9]{8} matches a string of eight hex characters
- Gr[ae]y matches both spellings of gray/grey
- ^(www\.example\.com|example.com)$ matches www.example.com and example.com
- ^[^~].*$ matches any line that does not start with ~
- \s\s+ matches multiple consecutive line breaks