Regular expressions are a very commonly used and powerful technology in work, but they are a bit boring to learn. Today, let me tell you how to write regular expressions in php?
Delimiter
When using the PCRE function, the pattern needs to be enclosed by a delimiter. The delimiter can be any non-alphanumeric, non-backslash, or non-whitespace character.
The commonly used delimiters are forward slash (/), hash symbol (#) and negation symbol (~). The following examples are all patterns using legal delimiters.
/foo bar/<br>#^[^0-9]$<br># php <br>%[a-zA-Z0-9_-]%
<br>
If a delimiter needs to be matched within a pattern, it must be escaped with a backslash. If delimiters occur frequently within the pattern, a better option is to use other delimiters to improve readability.
/http:\/\//<br>#http://
#When you need to put a string into a pattern for use, you can use preg_quote () function to escape it, its second parameter (optional) can be used to specify the delimiter that needs to be escaped.
In addition to the delimiters mentioned above, you can also use bracket-style delimiters. The left bracket and the right bracket serve as the start and end delimiters respectively.
{this is a pattern}
<br>
You can add pattern modifiers after the end delimiter. The following example is a case-insensitive match:
#[a-z]#i
<br>
## escape Sequence <span style="font-size: 20px;"></span>
$lng = 125.97097735211630;<br>
//Numbers that satisfy 14 decimal places
Backslash in expression<br>
In expression The backslash has multiple meanings, such as escaping, specifying a predefined character set, defining assertions, and displaying non-printable characters.
Escape characters<br>Escape characters mainly convert some special characters into ordinary characters. These commonly used special characters include ".", "?", "\", etc.
Specify the predefined character set
##Not displayed Printed characters
The above content is for reference only!
Recommended video tutorial:
PHP video tutorialThe above is the detailed content of How to write regular expression in php. For more information, please follow other related articles on the PHP Chinese website!