In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "regular expression summary commonly used in the project". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
A regular expression is a text pattern that consists 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 finding the body of the text. The regular expression acts as a template that matches a character pattern with the searched string.
The following editor summarizes some knowledge points about regular expressions, the details are as follows:
1. Metacharacter
[metacharacters with special meaning]
\ d-> matches a number of 0-9, which is equivalent to [0-9], as opposed to\ D-> matches any character except 0-9.
\ w-> matches a number or character of 0-9, amurz, Amurz, _, which is equivalent to [0-9aMurz Amurz]
\ s-> matches a white space character (space, tab.)
\ b-> match the boundary of a word
\ t-> matches a tab
\ n-> matches a newline
. -> matches any character except\ n
^-> begins with a metacharacter
$- > ends with a metacharacter
\-> transfer character
X | y-> x or y
[xyz]-> any one of x, y, z
[^ xyz]-> except for any character in xyz
[amurz]-> matches any character in amurz
[^ amurz]-> match except for any character in amurz
Grouping in ()-> regularity
Note:
1) about []
All the characters in parentheses in a, [+]-> represent their own meaning.
B, [12-65]-> this is not 12-65, but one of the three-- one-six-six-five.
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 what we think of as 18 or 19, but change to / ^ (18 | 19) $/ is a simple 18 or 19.
B, while capturing the content of the big regular match, the content of the packet match can also be captured-> packet capture
C. Group reference, for example: / ^ (\ d) (\ w)\ 2\ 1 $/, where\ 2 is the same as the second grouping, and\ 1 is exactly the same as the first grouping, for example: "0aa0"
[quantifier metacharacter representing quantity]
*-> 0 to multiple
+-> 1 to multiple
?-> 0 to 1
{n}-> appear n times
{n,}-> appear n to multiple times
{nmenthm}-> appear n to m times
Note:
1) about? Several situations of
A. Put it after the non-quantifier metacharacter to represent 0-1 occurrence.
B. Put it after the quantifier metacharacter to denote the greed of uncapturing, for example: reg=/\ dhammer reg=/; reg.exec ("2015")-> "2015" but if it is written as follows: reg=/\ dholder Acme; reg.exec ("2015")-> "2"
C. Add?: at the beginning of the group, which means that the current packet only matches and does not capture, for example: / ^ (?:\ d +) $/
D. Add? = at the beginning of the group and forward pre-check, for example: / ^ abcdef (? = 1 | 2) $/ only "abcdef1" and "abcdef2" match
E. Add?! at the beginning of the group, negative pre-check, for example: / ^ abcdef (?! 1 | 2) $/ except that "abcdef1" and "abcdef2" do not match, as long as it is "abcdef".
[metacharacters that represent one's own meaning]
Apart from the above, in the literal way, any other characters we appear represent their own meaning.
Var num=12
Var reg=/ ^\ w "+ num+" $/;-> here "+ num+" does not concatenate the values of variables, and whether it is "or +" are metacharacters.
-> for the way we need to concatenate strings and variables, we can only use instances to create rules.
2. Modifiers
I-> ignoreCase ignores the case of letters
G-> global global matching (plus g can solve the laziness of regular capture)
M-> multiline multiline matching
3. The rules commonly used in the project
1) significant numeric
Var reg=/ ^ [+ -]? (\ d | ([1-9]\ d +)) (\.\ d +)? $/
2) mailbox
Var reg = / ^\ w+ ((-\ w+) | (\.\ w+)) * @ [A-Za-z0-9] + ((\. | -) [A-Za-z0-9] +) *\. [A-Za-z0-9] + $/
3) telephone
Var reg = / ^ 1\ d {10} $/
4) those aged between 18 and 65
Var reg = / ^ ((18 | 19) | ([2-5]\ d) | (6 [0-5])) $/
5) Chinese name
Var reg = / ^ [\ u4e00 -\ u9fa5] {2pr 4} $/
6) ID card
Var reg = / ^ (\ d {6}) (\ d {4}) (\ d {2}) (\ d {2}) (?:\ d {2}) (\ d) (?:\ d | X) $/; / /-> 12828 (province, city and county) 1990 (year) 12 (month) 04 (day) 06 1 (odd number is male and even number is female) 7 (number or X)
4. Regular matching
Reg.test ([string])-> true means the match is successful false- > the match is not successful
5. Regular capture
1) reg.exec ([string])
-> match first. If the match succeeds in capture, an array is returned. If the match is not successful, null is returned.
-> regular capture is lazy and greedy
-> to solve laziness, add the global modifier g at the end of the rule
To solve the problem of greed, add it after the quantifier.
This is the end of the summary of regular expressions commonly used in the project. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.