Before learning regular expressions, we must first learn the delimiters of regular expressions.
The delimiter is to set a boundary, and what is within the boundary is the regular expression.
The regulations for PHP's regular expression delimiters are as follows:
delimiters cannot be used as a-zA-Z0-9\. Others can be used. They must appear in pairs, with a beginning and an end.
Let’s take a few examples:
Example | Explanation |
---|---|
/Write regular in the middle/ | Correct |
$Write regular in the middle$ | Correct |
%Write the regular expression in the middle% | Correct |
^Write the regular expression in the middle^ | Correct |
@Write regular in the middle@ | Correct |
(Write regular in the middle) | Error |
A is written in the middle of regular expression A | Error |
Note: \ is an escape character. If you need to match / in the regular expression later, as shown below:
/ / /
If you really want to match / at this time, you need to escape the / in the delimiter with an escape character and write it as the following example:
/ \/ /
If you find it troublesome, you can set two forward slashes (/ /) when encountering such characters that need to be escaped. delimiter, change it to another delimiter (# #).