In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Xiaobian to share with you the example analysis of regular expression grammar knowledge, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article. Let's learn about it!
Selection and grouping
(1)。 Grouping
Character group []: indicates that one of several characters is matched
The character group can be simply understood as a combination of some characters, the difference between the character group and ordinary characters is that the abc ordinary character means matching a, followed by b, and then c, while the character group [abc] means matching an or b or c in the same position; because the meaning of the character group itself determines that this character group can be regarded as an ordinary special character.
Generality: it is common because it also means that the operation of matching a character to a single character in a position can also work on it; for example: [a Muz] {6}
Particularity: special because, 1. Other characters may have different meanings inside and outside, such as: -,., ^, etc., 2. There can be multiple choices in the same location, but this choice is limited to one character.
The difference between each character inside and outside the character group:
-: within the character group, it represents a range; outside the character group, it represents only one character
Example:
-outside the character group
Var str = '2016-8-20 Belgian reg = / 16-8 Universe GmbH console.log (str.match (reg)); / 16-8
-within the character set
Var str = '2016-8-20 accounting reg = / 1 [6-8] / gorientation console.log (str.match (reg)); / / 16
-outside the character group is an ordinary character that only represents-the character itself, so the match is 1, followed by 6, followed by the string of 8; so it's 16-8.
-represents a range within the character group, so it matches 1, followed by any number between 6 and 8, so it's 16.
.: represents a dot within the character group; any character outside the character group (looks for a single character, except for newlines and line Terminators).
. Outside the character group
Var str = '2016-8-20 accounting reg = / 6.8Universe console.log (str.match (reg)); / / 6-8
. Within the character set
Var str = '2016-8-20 Belgian reg = / 6 [.] 8Universe GmbH console.log (str.match (reg)); / / null
. Represents any character outside the character group; so the match is 6, followed by any character, followed by a string of 8, so the match is 6-8.
. It's just an ordinary character within the character group that represents itself; so the match is 6, followed by. Next, a string of 8 is null because it does not match.
Character groups can also be divided into matching character groups and exclusive character groups, mainly because of the change in the meaning of ^.
^: delimiting characters
Example:
^ outside the character group
Var str = 'han';var reg = / ^ ha/g;console.log (str.match (reg)); / / ha
^ within the character set
Var str = 'han';var reg = / [^ 0-9] / gscape console.log (str.match (reg)); / / hjingajinn
Delimiting is an anchor outside [], indicating that it matches a character or string that begins with a certain character, except in [].
Note: [^ a] means to match a character whose position is not equal to a, do not understand that this position is not a can match, the latter understanding can match a blank line, pay attention to this difference
Example:
Var str = 'han';var reg = / n [^ Q] / ginto console.log (str.match (reg)); / / null
Note: this example does not have any characters after n, so the matching result is empty. If you follow the second understanding above, you may think that the matching result is: n
No matter how many characters there are in [], only any one of them will be matched in the same position. What if I want to match a string in one position?
(2)。 Choice
At this point, you can use () plus | this combination, which means "or" in the regular expression, while () divides a range in the regular expression; it is meaningless when used alone. It only works when it is used with other quantifiers (let's start with simple examples, greedy matching and non-greedy matching separately, without explanation here.)
Example:
Var str = '2016-8-20 consolation reg = / (16-8) / gscape console.log (str.match (reg)); / 16-8var str =' 2016-8-20 accountability.log (str.match (reg)); / 16-8
Whether there are parentheses or not in this example indicates that it matches the string 16-8.
When () is used with |, it is a multi-selection structure, which can match any subexpression () to represent a range; | indicates a selection direction
Example:
Var str = 'greygray';var reg = / Gr [ea] y gr console.log (str.match (reg)); / / grey,grayreg = / gr (e | a) y hand gash console.log (str.match (reg)); / / grey,gray
The result of the above two matches is grey, gray, that is to say, the branch selection structure of (|) can be replaced with [] to a certain extent.
Example:
Var str = 'greygray';var reg = / gre | ay | yg/g;console.log (str.match (reg)); / / gre,yg,ay
Output: gre,yg,ay
The addition of parentheses has a great impact on the results, such as:
^ hello | world | hi: matches ^ hello or world or hi
^ (hello | world | hi):. The starting line of the match is ^ hello or ^ world or ^ hi
Note: although multi-selection structures can sometimes be represented by character groups and they can represent each other, a character group can only match a single character in the target text, and each multi-selection structure itself may be a complete regular. Can match text of any length
A few points to pay attention to in regular expressions:
1. In regular expressions, a space character is also treated as a regular character
2. The meaning of Mustang * is not the same in [] and outside []. Pay attention to the difference between metacharacters inside and outside the character group.
3. When [] is added to several characters, the string is treated as a character group, and it operates on a single character. * etc. can also be used. The mixed use of quantifiers and the mixing of quantifiers and grouping choices will have a concept of greedy and non-greedy matching, which will be summarized separately.
4. The difference between a branch structure and a character group; a branch can match text of any length; a character group can match only one character
5. Pay attention to the understanding of some concepts: for example, [^ x] means' match a character that is not equal to x 'rather than' only if this position is not x'; the concept after [^ x] can match a blank line such as {xQuery y},? , * understanding
6. The key to the success of the match is the x _ () _ () _ (x) in {x _ (()) ~ () y}.
The above is all the contents of the article "sample Analysis of regular expression Grammar knowledge". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.