PHP regular expression function parsing and regular expression basic syntax

Time:2020-9-22

1、 PHP regular expression function analysis

PHP regular expression function parsing and regular expression basic syntax

PHP regular expression function parsing and regular expression basic syntax

PHP regular expression function parsing and regular expression basic syntax

PHP regular expression function parsing and regular expression basic syntax

PHP regular expression function parsing and regular expression basic syntax

PHP regular expression function parsing and regular expression basic syntax

PHP regular expression function parsing and regular expression basic syntax

PHP regular expression function parsing and regular expression basic syntax

PHP regular expression function parsing and regular expression basic syntax

PHP regular expression function parsing and regular expression basic syntax

PHP regular expression function parsing and regular expression basic syntax

PHP regular expression function parsing and regular expression basic syntax

PHP regular expression function parsing and regular expression basic syntax

PHP regular expression function parsing and regular expression basic syntax

PHP regular expression function parsing and regular expression basic syntax

preg_ match();preg_ match_ All (); form validation, etc

//Validity of email address – preg_ Match (), the matching result can be regarded as a bool value

preg_ Replace(); filtering of illegal words

//f**k

2、 Regular expression basic syntax

PHP regular expression function parsing and regular expression basic syntax

PHP regular expression function parsing and regular expression basic syntax

PHP regular expression function parsing and regular expression basic syntax

Tools for testing regular expressions

PHP regular expression function parsing and regular expression basic syntax

PHP regular expression function parsing and regular expression basic syntax

At noon, it’s better to convert to unicode:Unicode conversion tool

PHP regular expression function parsing and regular expression basic syntax
be careful:[^It must be together, otherwise it is as follows: ^ is also regarded as an atom to match.

Branching refers to making several rules. If any rule is satisfied, it will be regarded as successful matching. Specifically, the rules are separated by |, and the conditions are matched from left to right.
Tips
Due to the branch rule, as long as the matching is successful, the following conditions will not be matchedSo if you want to match content with inclusion relationships, pay attention to the order of the rules. Here is an example of using branching.

The rule of the U.S. Postal code is five numbers or five numbers with four numbers, such as 12345 or 54321-1234. If you want to match all the postcodes, the correct regular expression is as follows:
\d{5}-\d{4}|\d{5}
//Wrong way of writing
\d{5}|\d{5}-\d{4}
The following incorrect writing method can only match 5 digits and the first 5 digits of 9 digits, but not the 9 digit postcode.

PHP regular expression function parsing and regular expression basic syntax

PHP regular expression function parsing and regular expression basic syntax

.Equivalent to [^ n]

PHP regular expression function parsing and regular expression basic syntax

{n} 5 {3} is equivalent to 555 {n,}
For example: [a-za-z] {5,} letters with at least five consecutive occurrences of the preceding atom, such as wertt;
{n, m} means that the atom in front of it appears at least 5 times and at most m times, such as: W {5,9}
*Match 0, 1, or more times the atom before it, that is {0,} as w*
+Match the atom {1,} d before it once or more times+
? Match the atom {0,1} before it 0 or 1 times_ U?

PHP regular expression function parsing and regular expression basic syntax

PHP regular expression function parsing and regular expression basic syntax

Third, the revised model

PHP regular expression function parsing and regular expression basic syntax

PHP regular expression function parsing and regular expression basic syntax

takeMultiple possible outcomesThe shortest one, by default, is the greedy mode, which is the longest among various possibilities

PHP regular expression function parsing and regular expression basic syntax

PHP regular expression function parsing and regular expression basic syntax

PHP regular expression function parsing and regular expression basic syntax

x: Ignore spaces andTab tab blank

4、 Practical – common regular expressions

PHP regular expression function parsing and regular expression basic syntax

  1. Non null match:.+

  2. Floating point number:\d+\.\d{2}$

  3. Mobile phone number: ^ 1 (3 | 5) d {9}$

  4. Email:^\w+(\.\w+)*@\w+(\.\w+)+$

  5. URl:^(https?:\/\/)?(\w+\.)+[a-zA-Z]+$

  6. Match only 3-15 Chinese characters and uppercase and lowercase letters: ^ ([\x{4e00}-\x{9fa5}]|[a-zA-Z]){3,15}$/u

(/U means to match by Unicode (UTF-8) (mainly for multi byte, such as Chinese characters),/U is the lazy match)

The / of the regular expression is to be escaped. If it is not escaped, it will not match
Mode unit(3 | 5 | 7 | 8) is equivalent to [3578]
Note: (3|5) is equivalent to [35] instead of [3|5] Oh!!

Collation of common regular expressions in PHP