Home > Article > Backend Development > Organizing common regular expressions under PHP_PHP tutorial
-------------------------------------------------- ------- Regular collection
Mobile phone number:
$mode = "/^1[358]d{9}/";
Email address:
$mode = "/^[a-z][-_.]?[a-zd]*@[a-z0-9]+[.][a-z]{2,4}/i";
---------------------------------------------------------------- --------- Regular basis
$mode = "/^1[358]d{9}/i";
The matching module must start and end with / /, second You can add the pattern modifier
atom
①a-z A-Z _ 0-9 //the most common characters
②(abc) //the unit symbol enclosed in parentheses
③[abcs] [^abd] //Atomic table enclosed in square brackets,
^ in the atomic table represents exclusion or opposite content
d contains all numbers [0-9]
D Except all numbers [^0-9]
w Contains all English characters [a-zA-Z_0-9]
W Except all English characters [^a-zA-Z_0-9]
s contains blank areas such as carriage returns, line feeds, page breaks, etc. [fnr]
metacharacters
* matches 0 times, 1 or more times of the previous content
+ 1 or more times times
? 0 times or 1 time
. Represents any character (except carriage return and line feed)
| Equivalent to PHP's || (meaning "or")
^ Forces to match the first content of the string
$ Forcibly matches the content at the end of the string
[^abc] Matches content other than a or b or c
b Matches word boundaries, the boundaries can be spaces or special symbols
B Matches anything other than word boundaries Content
{m} matches the number of repetitions of the previous content M times
{m,} matches the number of repetitions of the previous content greater than or equal to M times
{m,n} matches the number of repetitions of the previous content M times to N times
( ) matches the whole and puts it into memory. You can use \1 or \2...to obtain
in sequence: reduce the
( ) parentheses in sequence because it is memory processing So the highest
*? + { } Repeat matching content second
^ $ b Boundary processing third
| Conditional processing fourth
Finally, calculate the match according to the order of operations
Common modifiers: $mode = "/regular /U";
i Regular content is not case-sensitive when matching (default is case-sensitive)
m Use multi-line recognition matching when matching the first or last content
S Convert carriage returns into spaces
x ignores whitespace in the regular expression
A forces matching from the beginning
D forces $ to match without any content at the end n
U prohibits greedy matching, only traces to the nearest matching character and ends,
Regular expressions commonly used in collection programs
Application
preg_match_all (string pattern, string subject, array matches [, int flags])
Intercept more detailed content, collect web pages, and analyze text
preg_replace ( mixed pattern, mixed replacement, mixed subject [, int limit] )
preg_replace ( mixed pattern, mixed replacement, mixed subject [, int limit] )
Tip 1. The replacement content can be a regular expression It can also be an array regular
2. The replacement content can be solved by the modifier e. The replacement execution content
preg_split ( string pattern, string subject [, int limit [, int flags]] )
through regular expressions To cut related content, it is similar to the explode cutting function learned before, but explode
can only cut in one way and has limitations.
-------------------------------------------------- -- Debug code
[code]
$mode = "/^[a-z][-_.]?[a-zd]*@[a-z0-9]+ [.][a-z]{2,4}/i";
$str = "a12345@jb51.net";
echo $str.'