In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you what the three matching patterns provided by JDK in Java are, I believe most people don't know much about it, so share this article for your reference. I hope you can learn a lot after reading this article. Let's learn about it together.
Matching pattern
JDK provides three matching patterns, namely: greedy pattern (greedy), reluctant pattern (reluctant) and possessive pattern (possessive), which correspond to three kinds of possessive quantifiers, of which greedy pattern is the default pattern, and grudging pattern by adding one after the expression? To show. The possession pattern is represented by adding a + to the expression.
What is the meaning of the three models?
The meaning of the greedy pattern is to match as many matches as possible, while also trying to satisfy the overall matching.
The meaning of the grudging pattern is to match as few matches as possible, while also trying to satisfy the overall matching.
The meaning of the possession pattern is that there are as many matches as possible, and if the collation cannot match because of too many matches, then there will be no backtracking.
For example, a string is as follows:
/ m/t/wd/nl/n/p/m/wd/nl/n/p/m/wd/nl/n/p/m/v/n
Expression matching for greedy patterns:
/ m/t.*/nl/n/p/m
In this case, the matching result is / m/t/wd/nl/n/p/m/wd/nl/n/p/m/wd/nl/n/p/m
Expressions of grudging patterns match:
/ m/t/.*?/nl/n/p/m
In this case, the matching result is / m/t/wd/nl/n/p/m
/ m/t/wdx+?/nl/n/p/m
If so, then the match will not match, because + means at least one match, reluctantly one pattern, at least one match, so the match failed.
Expression matching of occupancy patterns:
/ m/t.*+/nl/n/p/m cannot match at this time because. * too many characters have been matched so that it cannot be matched later.
Note: only reluctant or possessive quantifiers can be used for variable matching rules. Like Xerox? Means to match the character X as little as possible, and X? But it is the default greed mode, which means to match as much as possible. Another example: X {n} means that you must be prepared to match n Xs, then adding other quantifiers will not work.
Look around (forecast)
Looking around is a more advanced topic, but it is so natural to use.
Looking around applies to scenarios where when doing regular matching, you need to know whether there are specific expressions before or after the matched part, with or without, without capturing (consuming) these specific expressions.
If you do not look around, but directly use expressions to judge, it will inevitably cause these matched expressions to be consumed.
For example: suppose I want to break the sentence ILoveYou, the principle is that the emergence of capital letters is considered to be a new word.
If you use this matching rule:
\ p {Upper}\ p {Lower} * [\ p {Upper}]?
If so, the matched capital letters will be consumed. The matching result will be:
IL
You
This does not meet the requirements.
The solution is to look around, and the regular expression is:
\ p {Upper}?\ p {Lower} * (? = [\ p {Upper}]?)
The output is as follows:
I
Love
You
There are four kinds of looking around:
(? = X) means that the regular expression X is followed by the regular expression X, which is not consumed and captured when the previous part is matched. Zero width positive positive prediction.
(X) this has not been studied clearly.
The above is all the content of the article "what are the three matching patterns provided by JDK in Java". 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.