PHP Regular Expressions
What is a Regular Expression
A regular expression is a search pattern made up of a series of characters. This search pattern can be used to define what you're looking for while searching for data in a text.
A regular expression is a pattern that can be simple as single letter or as complex pattern.
All forms of text search and replace operations may be performed with regular expressions.
Syntax :- Strings made of delimiter characters, a pattern and optional modifiers make up a regular expression in the PHP language.
$exp = "/hello/i";
In the previous example, /
is the delimiter, simmanchith is the pattern searched for and i
is a search insensitive modifier.
Any character that isn't a letter, number, backslash, or space can be used as a delimiter. The forward-slash(/) is the most frequent delimiter, although if your pattern incorporates forward slashes, alternative delimiters such as # or ~ are more useful.
Regular Expression Functions
Regular expressions are supported by a number of PHP functions. The methods preg_match()
, preg_match_all()
, and preg_replace()
are among the most regularly utilised :
Function | Description |
---|---|
preg_match() | If the pattern was discovered in the string, 1 is returned; otherwise, 0 is returned. |
preg_match_all() | If the pattern was detected just once in the string, it will return0; otherwise, it will return 1. |
preg_replace() | Replacing of pattern-matching strings with another string is returned. |
Using preg_match()
The preg_match()
method will notify you if a string includes pattern matches.
Example :- To search for "simmanchith" in a string without regard to case, use a regular expression:
Output :-
preg_match_all()
The preg_match_all()
method returns the number of matches found in a string for a given pattern.
Example :- To count the number of instances of "ain" in a string using a regular expression, perform the following :
Output :-
Using preg_replace()
The preg_replace()
method will substitute or replace a string for change all matching patterns.
Example :- To replace Microsoft with simmanchith in a string, use a casual regular expression :
Output :-
Related Links
Regular Expression Modifiers
Modifiers can alter the way a search is carried out.
Modifier | Description |
---|---|
i | case-insensitive search is performed. |
m | A multiline search is performed (Beginning and ending patterns will match the beginning and ending of every line). |
u | Allows UTF-8 encoded patterns to be correctly matched. |
Regular Expression Patterns
Brackets are used to find for a specific set of characters :
Expression | Description |
---|---|
[abc] | Between the brackets, choose one character from the list. |
[^abc] | Look for any character that isn't in the brackets. |
[0-9] | character can you find in the range of 0 to 9. |
Metacharacters
Metacharacters are characters that have a specific significance :
Metacharacter | Description |
---|---|
| | Look for a match for any of the patterns separated by | , such as cat|dog|fish. |
. | Find only one character in each category. |
^ | When a string begins with, it is matched ex: ^Hello. |
$ | Searches for a match at the end of a string: like World$ |
\d | Search a digit |
\s | Search a whitespace character |
\b | Look for a match at the start of a word like this: b\WORD, or at the ending of a phrase such as this: WORD\b |
Related Links
Quantifiers
define the quantities of Quantifiers :
Quantifier | Description |
---|---|
n+ | Any string with at least one n will be matched. |
n* | Any string with zero or more instances of n is matched. |
n? | Any string with zero or one instances of n is matched. |
n{x} | Any string with a series of X n's will be matched. |
n{x,y} | Any string with a sequence of X to Y n's will be matched. |
n{x,} | Any string containing at least X n's will be matched. |
Note : You can use a back slash (\) to escape your phrase when you have to search for one of the special letters. For example, the following phrase may be used to search for one or more question marks : $pattern = '/?+/';
Grouping
To apply quantifiers to whole patterns, use parentheses ( )
. They can also be used to choose which elements of the pattern to match.
Example :- Search for the word "banana" by searching for followed by two cases :
Output :-