PHP Regular Expressions
A regular expression is a sequence of characters that forms a search pattern. When you search for data in a text, you can use this search pattern to describe what you are searching for.
A regular expression can be a single character, or a more complicated pattern.
Regular expressions can be used to perform all types of text search and text replace operations.
Regular Expression Syntax
In PHP, regular expressions are strings composed of delimiters, a pattern and optional modifiers.
Syntax
"/pattern/modifiers"
- delimiters - characters that enclose the pattern (e.g.
/) - pattern - the character sequence to search for
- modifiers - how the search is performed (e.g.
iindicates a case-insensitive search)
Look at the following regular expression:
$exp = "/example/i";
Here, / is the delimiter, example is the patternto search for, and i is a modifierthat makes the search case-insensitive.
The delimiter can be any character that is not a letter, number, backslash or space. The most common delimiter is the forward slash (/), but when your pattern contains forward slashes it is convenient to choose other delimiters such as # or ~.
Regular Expression Modifiers
The modifiers specify how the search is performed.
| Modifier | Description | Try it |
|---|---|---|
| i | Performs a case-insensitive search | |
| m | Performs a multiline search (patterns that search for a match at the beginning or end of a string will now match the beginning or end of each line) | |
| u | Enables correct matching of UTF-8 encoded patterns |
Regular Expression Patterns
Brackets are used to find a range of characters:
| Expression | Description | Try it |
|---|---|---|
| [abc] | Find one or many of the characters inside the brackets | |
| [^abc] | Find any character NOT between the brackets | |
| [a-z] | Find any character alphabetically between two letters | |
| [A-z] | Find any character alphabetically between a specified upper-case letter and a specified lower-case letter | |
| [A-Z] | Find any character alphabetically between two upper-case letters. | |
| [123] | Find one or many of the digits inside the brackets | |
| [0-5] | Find any digits between the two numbers | |
| [0-9] | Find any digits |
Metacharacters
Metacharacters are characters with a special meaning:
| Metacharacter | Description | Try it |
|---|---|---|
| | | Find a match for any one of the patterns separated by | as in: cat |
| . | Find any character | |
| ^ | Finds a match as the beginning of a string as in: ^Hello | |
| $ | Finds a match at the end of the string as in: World$ | |
| \d | Find any digits | |
| \D | Find any non-digits | |
| \s | Find any whitespace character | |
| \S | Find any non-whitespace character | |
| \w | Find any alphabetical letter (a to Z) and digit (0 to 9) | |
| \W | Find any non-alphabetical and non-digit character | |
| \b | Find a match at the beginning of a word like this: \bWORD, or at the end of a word like this: WORD\b | |
| \uxxxx | Find the Unicode character specified by the hexadecimal number xxxx |
Quantifiers
Quantifiers define quantities:
| Quantifier | Description | Try it |
|---|---|---|
| n+ | Matches any string that contains at least one n | |
| n* | Matches any string that contains zero or more occurrences of n | |
| n? | Matches any string that contains zero or one occurrences of n | |
| n{3} | Matches any string that contains a sequence of 3 n's | |
| n{2, 5} | Matches any string that contains a sequence of at least 2, but not more that 5 n's | |
| n{3,} | Matches any string that contains a sequence of at least 3 n's |
Note: If your expression needs to search for one of the special characters you can use a backslash ( ) to escape them. For example, to search for one or more question marks you can use the following expression: $pattern = '/?+/';