In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
I would like to share with you an example analysis of zero-width assertions of regular expressions. I believe most people don't know much about it, so share this article for your reference. I hope you will learn a lot after reading this article. Let's take a look at it!
Regular expression zero width assertion:
Zero-width assertion is a difficult point in regular expressions, so this chapter focuses on the analysis of matching principle. Zero-width assertions have other names, such as "look around" or "pre-search", but these are not the focus of our attention.
one。 Basic concepts:
A zero-width assertion, like its name, is a zero-width match, and the content it matches is not saved in the matching result, and the final matching result is just a location.
The function is to add a qualification to the specified position, which is used to specify that the characters before or after this position must meet the qualification in order for the word expression in the rule to match successfully.
Note: the subexpression here is not just an expression enclosed in parentheses, but any matching unit in a regular expression.
Javascript only supports zero-width advance assertions, and zero-width advance assertions can be divided into positive zero-width advance assertions and negative zero-width advance assertions.
The code example is as follows:
Example code 1:
Var str= "abZW863"; var reg=/ab (? = [Amurz]) /; console.log (str.match (reg))
In the above code, the semantics of the regular expression is to match the string "ab" followed by any uppercase letter. The final match result is "ab" because the zero-width assertion "(? = [Amurz])" does not match any characters, but is used to specify that the current position must be followed by an uppercase letter.
Example code 2:
Var str= "abZW863"; var reg=/ab (?! [Amurz]) /; console.log (str.match (reg))
In the above code, the semantics of the regular expression is to match the string "ab" that is not followed by any uppercase letter. The regular expression does not match any characters because in the string, ab is followed by uppercase letters.
two。 Matching principle:
The above code only conceptually describes how zero-width assertions match.
Here's how the positive and negative zero-width assertions match in terms of the matching principle.
1. Positive zero width assertion:
The code example is as follows:
Var str= "antzone"; var reg=/ ^ (str.match (reg))
The matching process is as follows:
First, the control is obtained by the "^" in the regular expression, and the match starts with position 0, which matches the starting position 0, and the match is successful, and then the control is transferred to "(? ="
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.