Home  >  Article  >  Web Front-end  >  Summary of regular expressions (practical summary)

Summary of regular expressions (practical summary)

php中世界最好的语言
php中世界最好的语言Original
2018-03-30 13:42:241393browse

This time I will bring you a summary of regular expressions (practical summary), what are the precautions for using regular expressions in practice, the following is a practical case, let’s take a look .

Regular expression is a text pattern composed of ordinary characters (such as characters a to z) and special characters (called metacharacters). The pattern describes one or more strings to be matched when searching for text bodies. Regular expressions serve as a template that matches a character pattern with a searched string.

The editor below summarizes some knowledge points about regular expressions. The specific content is as follows:

1. Metacharacters

[Metacharacters with special meanings]
\d -> matches a number from 0 to 9, equivalent to [0-9], and its opposite is \D -> matches a number except Any character from 0-9
\w -> Matches a number or character from 0-9, a-z, A-Z, _, equivalent to [0-9a-zA-Z_]
\s -> Matches A whitespace character (space, tab...)
\b -> Matches a word boundary
\t -> Matches a tab character
\n -> Matches a newline
. -> Matches any character except \n
^ -> Begins with a certain metacharacter
$ -> Ends with a certain metacharacter
\ -> Transfer Character
x|y -> One of x or y
[xyz] -> Any one of x, y, z
[^xyz] -> Except any one of xyz
[a-z] -> Matches any character in a-z
[^a-z] -> Matches any character except a-z
() -> Grouping in regular expressions

Note:

1) Regarding []

a, [+] ->All characters appearing in square brackets represent their own meaning
b. [12-65] ->This is not 12-65 but one of the three 1/2-6/5

2) About ()

a. The function of grouping is to change the default priority, for example: /^18|19$/, 181, 189, 119, 819, 1819... all match, not 18 or 19 as we think, But changing it to /^(18|19)$/ is simply 18 or 19
b. While capturing the content of the regular match, you can also capture the content of the group match ->Group capture
c. Group reference, for example: /^(\d)(\w)\2\1$/, where \2 is exactly the same as the second group, and \1 is exactly the same as the first group. The content, for example: "0aa0" is consistent with

[quantifier metacharacter representing quantity]

* -> 0 to multiple
+ -> 1 to multiple
? -> 0 to 1
{n} -> appears n times
{n,} -> appears n to multiple times
{n,m} -> appears n to m times

Note:

1) Several situations about ?

a. Place it after the non-quantifier metacharacter to represent the occurrence 0-1 times
b. Place it after the quantifier metacharacter to represent the greediness when canceling the capture, for example: reg=/\d+/; reg.exec("2015") -> "2015" But if the regular Write like this reg=/\d+?/; reg.exec("2015") -> "2"
c. Add ?: at the beginning of the group, which means that the current group only matches and does not capture, for example:/^ (?:\d+)$/
d. Add ?= at the beginning of the group to perform forward search, for example: /^abcdef(?=1|2)$/ Only "abcdef1" and "abcdef2" are consistent
e. Add ?! at the beginning of the group, negative pre-check, for example: /^abcdef(?!1|2)$/ Except "abcdef1" and "abcdef2" do not match, the others as long as it is "abcdef (any things)" are consistent with

[Metacharacters representing their own meaning]

In addition to the above, in the literal mode, any other characters we appear represent their own meaning

var num=12;
var reg=/^\w"+num+"$/; ->Here "+num+" does not splice the value of the variable, and whether it is " or + They are all metacharacters

->For the method that requires splicing strings and variables, we can only use the instance method to create regular expressions

2 and modifiers

i -> ignoreCase ignores the case of letters
g -> global global matching (adding g can solve the laziness during regular capture)
m -> multiline multiline matching

3. Regular rules commonly used in projects

1)

var reg=/^[+-]?(\d|([1-9]\d+))(\.\d+)?$/;

of valid digits 2)

 var reg = /^\w+((-\w+)|(\.\w+))*@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
## of the email address # 3) Phone number

 var reg = /^1\d{10}$/;
4) Age between 18-65

 var reg = /^((18|19)|([2-5]\d)|(6[0-5]))$/;
5) Chinese name

 var reg = /^[\u4e00-\u9fa5]{2,4}$/;
6) ID card

 var reg = /^(\d{6})(\d{4})(\d{2})(\d{2})(?:\d{2})(\d)(?:\d|X)$/;
 //-> 12828(省市县) 1990(年) 12(月) 04(日) 06 1(奇数是男偶数是女) 7(数字或者X)

4. Regular matching

reg.test([string]) ->true means successful matching false->unsuccessful matching

5. Regular capture

1)reg.exec([string])

-> First match, the match is successful During capture, an array is returned; if the match is unsuccessful, null is returned;

-> Regular capture is lazy and greedy
-> To solve laziness, add the global modifier g## at the end of the regular expression # -> To solve greediness, add ?

after reading the case in this article. I believe you have mastered the method. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

Detailed explanation of the use of regular pattern modifiers

##What are the new features in regular expressions

The above is the detailed content of Summary of regular expressions (practical summary). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn