Regular expression summary

xiaoxiao2021-03-05  24

Regular expressions are mainly used to analyze text content, especially suitable for verification data.

^ Symbol match the beginning of the string.

For example: ^ ABC matches "ABC XYZ" without matching "XYZ ABC"

The symbol matches the end of the string.

For example: ABC $ matches "XYZ ABC" without matching "ABC XYZ". Note: If you use the ^ symbols and $ symbols at the same time, it will be accurately matched.

For example: ^ ABC $ only matches "ABC"

* The symbol matches 0 or more front characters.

For example: AB * can match "ab", "abb", "abbb", etc.

The symbol matches at least one front character.

For example: AB can match "ABB", "ABBB", etc., but do not match "ab".

? The symbol matches 0 or 1 front characters.

For example: AB? C? Can only match "ABC", "ABBC", "ABCC" and "ABBCC"

The symbol matches any character other than the change line.

For example: (.) ​​ Match all strings other than the restroom

X | Y matches "x" or "y".

For example: ABC | XYZ can match "ABC" or "XYZ", and "AB (C | X) Yz" matches "abcyz" and "ABXYZ"

{n} matches characters in front of N times (n is non-negative integer).

For example: A {2} can match "AA", but do not match "a"

{n,} Match the character in front of at least N (n is non-negative integer).

For example: a {3,} matches "AAA", "AAAA", etc., but does not match "a" and "aa". Note: A {1,} equivalent to A a {0,} equivalent to a *

{m, N} matches at least M, up to N, and the characters.

For example: A {1,3} matches "A", "AA" and "AAA". Note: A {0,1} is equivalent to a?

[XYZ] represents one of the character sets that matches one of the characters in parentheses.

For example: [ABC] matches "A", "B" and "C"

[^ xyz] represents a negative character set. Match any character in this parentheses.

For example: [^ ABC] can match any characters other than "A", "B" and "C"

[A-Z] indicates a range of characters in a range, matching any characters in the specified interval.

For example: [A-Z] matches any lowercase letter character between "A" to "Z"

[^ m-n] represents characters outside a range, matching characters that are not within the specified range.

For example: [m-n] matches any character from "M" to "N"

/ Symbol is a escape operator.

For example: / N wrap / F pacharge / R Enter // Matter // Match "/" // Match "/" / s any white character, including spaces, tabs, Page break, etc. Equivalent to "[/ f / n / r / t / v]" / s any non-blank characters. Equivalent to "^ / f / n / r / t / v]" / w word characters, including letters and underscores. Equivalent to "[A-ZA-Z0-9_]" / w any non-word characters. Equivalent to "[^ A-ZA-Z0-9_]" / B match the end of the word. For example: VE / B match the word "love", etc., but do not match "Very", "Even", etc.

/ B matches the beginning of the word.

For example: VE / B match the word "Very", but do not match "love", etc.

/ d Match a numeric character, equivalent to [0-9].

For example: ABC / DXYZ matches "abc2xyz", "abc4xyz", etc., but does not match "abcaxyz", "ABC-XYZ", etc.

/ D Match a non-digital character, equivalent to [^ 0-9].

For example: ABC / DXYZ matches "abcaxyz", "abc-xyz", etc., but does not match "ABC2xyz", "ABC4xyz", etc.

/ NUM matches NUM (where NUM is a positive integer), reference to the match to remember.

For example: (.) ​​/ 1 Match two consecutive identical characters.

/ ONUM matches N (where N is an octave extension value of one less than 256).

For example: / O011 matching tab

/ XNUM matches NUM (where Num is a hexadecimal code value of less than 256).

For example: / x41 match characters "a"

Through the example of the example above, you can write down the following common regular expressions.

1) Domestic phone:

^ / D {3, 4} / -? / d {6,8} $

Match 021-52540753, 0798-678901, do not match 09888-0900000, 098-56787

2) Email

^ [^ @] * @ [^ @] * $ (Not very accurate, throwing jade :))

3) Domestic Postal Code

^ [1-9] / d {5} $

4) A precise digital verification

^ (([1-9] ([0-9] *)? (/./ d )?) | 0 | (0 /. D )) $

Match 7, 89, 700, 7.9, 0.8, 0 does not match 07, 08000, 89., 0.,00.

The above information is only for verification reference, if you want to get more, you can refer to the relevant documentation.

转载请注明原文地址:https://www.9cbs.com/read-36212.html

New Post(0)