In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how regular expressions achieve lazy matching patterns. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
one。 The concept of inert mode:
This pattern is the opposite of the greedy pattern, which matches as few characters as possible to satisfy regular expressions, for example:
Var str= "axxyyzbdkb"; console.log (str.match (/ a.*b/))
The above code is in greedy mode, so it can match the entire string, so let's modify it to lazy matching pattern:
Var str= "axxyyzbdkb"; console.log (str.match (/ a.*?b/))
The above code is lazy matching by adding a question mark (?) after the repeating quantifier. That's it.
The lazy matching pattern is to match as few characters as possible, but it must meet the matching rules of the regular expression, such as the code above. * can repeatedly match 0 or more previous characters or subexpressions, but the regular expression must end with b, so the regular expression can match the axxyyzb in the string above.
The summary is as follows:
1. Add a question mark (?) after the repeated quantifier. An inert match can be formed.
two。 Lazy matching matches as few characters as possible, but the entire matching pattern must be satisfied.
two。 List of lazy qualifiers:
Semantic interpretation of grammatical structure *? You can repeat as many times as you like, but as few times as possible. +? You can repeat one or any number of times, but as few times as possible, but at least 1. ?? You can repeat 0 or 1 times, but repeat as little as possible. {n,m}? You can repeat n to m here, but repeat as little as possible, and the minimum number of matches is n. {n,}? You can repeat more than n times, but repeat as little as possible, at least match this.
Brief introduction
In fact, greed and inertia are easy to understand, and we can see from the literal meaning that the so-called "greed" means that if it meets the requirements, it will match until it cannot be matched, and this is the greedy pattern. The so-called lazy pattern is the end of the match as soon as the match is appropriate, and the match is no longer going on. Here are a few examples to focus on.
First of all, let's talk about the identifier of the greed pattern: +,? , *, {n}, {n,}, {n ·m}. Inert mode: +? ,?? , *? , {n}?, {n,}?, {n,m}?
Example one
Var pattern=/8 [a-zA-Z0-9] * 7max; greedy mode var string= "abc8defghij7klngon8qrstwxy7"
At this time, the greedy mode * is used, which means that there can be any number of letters between 8 and 8, then the rule matches the first 8 first, and if it matches, it matches the following content indefinitely, as long as the latter content satisfies [a-zA-Z0-9]. Keep matching until you can no longer match, see if the next one is 7, if not, then he will move forward (spit out one to see if it is 7), and if not continue to spit out 7, and then match the content in between. So the result matches the whole string.
Var pattern=/8 [a-zA-Z0-9] *? 7 zigs; inert mode var string= "abc8defghij7klngon8qrstwxy7"
The inert pattern *? is used in the above rule. In this case, match an 8 first, and then match a character later to see if it matches [a-zA-Z0-9]. If it does, see if the next character is 7. If it is 7, it ends. If not, match another character later to see if it matches [a-zA-Z0-9]. If it matches, it matches [character-9]. Let's see if the next character is 7. If it is 7, it ends. Otherwise, follow the above way to cycle through, until the instructions are met.
(2)。 The patterns of greed and inertia can be expressed in another way.
Example two
Var test= "
"; var pattern=/
] *\ / > / ig
The lazy pattern can also be implemented, and [^ >] means that there is no > in between, so the result can be found for each
Label.
This is the end of the article on "how regular expressions achieve lazy matching patterns". I hope the above content can be helpful to you so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.