Regular expressions are a very important knowledge point in PHP, often used to find and replace strings, the most common is to verify that the user entered the information format is correct, such as message format, phone format, and so on. There are also such as collectors of software, is also necessary to use!
Now start to learn the basic syntax of regular expressions:
1. "/" is a delimiter, the part between the "/" delimiter is the pattern that will be matched in the target object. in order to be more flexible at the same time, the introduction of the meta character, that is, "+", "*", and "?".
(1) "+" metacharacters specify that their leading characters must appear consecutively or repeatedly in the target object
For example:/php+/, can match "phpp", that is, the letter after the ph of a continuous letter of one or more letters p of the string match.
(2) "*" metacharacters specify that their leading characters must appear 0 or more consecutive times in the target object
For example:/php*/can be matched with "PHPDDT", that is, there can be 0 or more p after the ph
(3) "?" Metacharacters specify that its leading object must appear 0 or one consecutive times in the target object.
For example:/php?/can "Pher" match, that is, PHH can be followed by 0 or 1 p
Other important metacharacters:
- \s: Used to match a single spaces, including tab keys and line breaks;
- \s: Used to match all characters except a single spaces;
- \d: Used to match numbers from 0 to 9;
- \w: Used to match letters, numbers, or underscore characters;
- \w: Used to match all characters that do not match the \w;
- . : Used to match all characters except for line breaks.
Example:/\s+/is used to match one or more space characters in the target object
2. The locator is used to specify where the match pattern appears in the target object. Commonly used there are "^", "$", "\b" and "\b"
(1) "^" Locator specify that the match pattern must be present at the beginning of the target string
(2) "$" to specify that the match pattern must be present at the end of the target object
(3) \ The B-Locator stipulates that the matching pattern must now be one of the two boundaries at the beginning or end of the target string. The
(4) "\b" Locator stipulates that the matching object must be within two boundaries at the beginning and end of the target string
3.php's regular matching mode is very flexible, You can specify a range of
for example:
/[a-z]/
The above regular expression will match any uppercase letter from a to Z range.
/[a-z]/
The regular expression above will match any lowercase letter from a to Z range.
/[0-9]/
The regular expression above will match any number in the range 0 through 9.
/([a-z][a-z][0-9]) +/
The above regular expression will be associated with any string of letters and numbers
4. Can be matched to multiple modes at the same time
such as/phpddt.com| Phpddt|100/can match the "phpddt.com" PHPDDT "100"
5. The negative "[^]" stipulates that the string specified in the pattern cannot exist in the target object
for example: [^PHPDDT] Match everything except the PHPDDT character
Here's a regular expression of common functions! (very important)
<?php//preg_match ("Regular expression", "string") is used to find a match in a string $email = "987044391@qq.com"; if (Preg_match ("/^" ([a-za-z0-9]) + ([. a-za-z0-9_-]) *@ ([. a-za-z0-9_-]) + ([. a-za-z0-9_-]+) + ([. a-za-z0-9_-]) $/", $email
) {echo ' matched successfully
The above is the entire content of this article, I hope that the learning of PHP regular expressions help.