Regular expression collation is commonly used in PHP

  • 2020-03-31 21:20:38
  • OfStack

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- regular collection

Mobile phone number:
$mode = "/ ^ 1 [358] \ d {9} /";

Email address:
$mode = "/ ^ [a-z] [- _ \.]? [a-z \] d * @ [a - z0-9] + [\.] [a-z] {2, 4} / I ";

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- a regular basis

$mode = "/ ^ 1 [358] \ d {9} / I";
Matching modules must start and end with / /, and the second/can be followed by a pattern modifier

atomic
A- z a-z _ 0-9 // the most common character
(ABC) // unit symbol enclosed in parentheses
[abcs] [^abd] // the table of atoms enclosed in square brackets,
The ^ in the atomic table represents the exclusion or opposite

\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 return, line feed, pagination, etc. [\f\n\r]

metacharacters
* matches 0 times or more of the previous content
Plus one or more times
? Zero or one
. Represents any character (except carriage return)
| is equivalent to PHP || (or)
^ force a match to the beginning of the string
$forces a match to the end of the string
[^ ABC] matches anything but a or b or c
\b matches word boundaries, which can be Spaces or special symbols
\B matches except with word boundaries
{m} matches the previous content m times
The number of times {m,} matches the previous content is greater than or equal to m
{m,n} matches the number of iterations of the previous content from m to n
() whole match, and put into memory, can be used with \\1 or \\2... In turn get

Priority: reduced in order
() parentheses are the highest because they are memory processed
*? + {} repeat match content next
^ $\b boundary processing third
| conditional treatment number four
Finally, the matching is calculated in order of operation

Common modifier: $mode = "/ regular /U";
I regular content is case-insensitive when matching (the default is case-insensitive)
M adopts multi-line recognition matching when matching the first or last content
S converts the carriage return to space
X ignores the whitespace in the regularization
A forces A match from scratch
D forces $to match with nothing in the tail \n
U prohibits greedy matching, tracks only the nearest match and ends,
A regular expression commonly used on a collector

application
Preg_match_all (string pattern, string subject, array matches [, int flags])
Intercept more detailed content, collect web pages, 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 or an array regular
2. The replacement content can be solved by modifying the character e to replace the execution content
Preg_split (string pattern, string subject [, int limit [, int flags]])
Using regular expressions to cut the content, similar to the unbounded cut function we learned before, but unbounded
There are limitations to being able to cut in one way.
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - to debug the code
[code]
< ? PHP
$mode = "/ ^ [a-z] [- _ \.]? [a-z \] d * @ [a - z0-9] + [\.] [a-z] {2, 4} / I ";
$STR = "a12345@jb51.net";
Echo $STR. "< Hr> ';
If (preg_match ($mode, $STR, $arr)) {
Echo 'succeed - < Font color = red> $arr. '[0];
} else {
Echo 'failed';
}
? >
[code]

Related articles: