Regular Expression functions allow you to search specific patterns in the string and replace them with some other pattern/string.
On this page, you will find the regular expression functions, their relative information like special characters/symbols, and their meanings.
Function | Description |
---|---|
preg_filter() | Get string or array with pattern matches replaced, but only if matches were found |
preg_grep() | Get an array of elements from the input array which matched the pattern |
preg_last_error() | Get an error code showing the reason that the last regular expression call failed |
preg_match() | Get the first match of a pattern in a string |
preg_match_all() | Get all matches of a pattern in a string |
preg_replace() | Get strings were matches of a pattern replaced with a substring. The substring value is returned by the callback function. |
preg_replace_callback() | Get string where all matches of the expression are replaced with the substring. The substring value is returned by the callback function. |
preg_replace_callback_array() | Get a string where all matches of each expression are replaced with the substring. The substring value is returned by the callback function. |
preg_split() | Break the string based on the regular expressions and return the partitions of the string as array |
preg_quote() | Put a backslash in front of characters that have a special meaning in regular expressions |
Modifiers in Regex
Modifiers define the behavior of searching for patterns in the string.
Modifier | Description |
---|---|
i | case-insensitive search for the pattern |
m | Search in multiline. That is, search for a matching pattern at the beginning or end of each line |
u | Enables correct matching of UTF-8 encoded patterns |
Regex Patterns
Brackets define the range of characters to create patterns.
[abc] | Find a character from the items between the brackets |
[^abc] | Find character NOT between the brackets |
[0-9] | Find a character from 0 to 9 |
Regex Metacharacters
Metacharacters are characters with a special meaning:
Metacharacter | Description |
---|---|
| | Look for a match for any one of the patterns separated by | |
. | Look for one instance of any character |
^ | Look for a match as the beginning of a string |
$ | Find match at the end of the string |
\d | Find a digit |
\s | Find a whitespace character |
\b | Find a match at the beginning of a word |
\uxxxx | Find the Unicode character given by the hexadecimal number XXXX |
Quantifiers in Regex
Quantities are defined by quantifiers.
Quantifier | Description |
---|---|
n+ | Find string containing at least one n |
n* | Find string containing zero or more occurrences of n |
n? | Find string containing zero or one occurrence of n |
n{x} | Find string containing sequence of X n‘s |
n{x,y} | Find string containing sequence of X to Y n‘s |
n{x,} | Find string containing sequence of at least X n‘s |