In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how regular expressions are used in sub-expressions. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
The details are as follows:
Note: in all examples, the regular expression matching result is included between [and] in the source text, and some examples will be implemented using java. If it is the regular expression usage of java itself, it will be explained in the appropriate place. All the java examples passed the test under JDK1.6.0_13.
I. introduction of questions
First of all, let's take a look at an example. Some phrases such as Windows 2000 are composed of multiple words, but they are actually a whole. A HTML page can use a non-wrapping space (that is, non-breaking space) to display it on a line in the browser. Now match multiple such spaces:
Text: Your operation systemis Windows 2000.
Regular expression: nbsp; {2,}
Results: Your operation systemis Windows 2000.
Analysis: the pattern is used here to match two or more non-wrapping spaces, but as you can see from the results, there is nothing to match, because the pattern nbsp; {2,} can only match text like nbsp; that starts with nbsp and has two or more consecutive semicolons.
Because the repeated matching mentioned earlier is the repetition of the character immediately before the repeated matching metacharacter, but what if we want to match a string multiple times?
2. Sub-expression
We derive the subexpression from the above. A subexpression is part of a large expression, and the purpose of dividing an expression into multiple subexpressions is to use those subexpressions as a separate element. Subexpressions must be enclosed in (and). Therefore, in the previous example, the regular expression should be written as (nbsp;) {2,}.
Let's look at a pattern that matches the valid year:
Text: 1988-11-13
Regular expression: (19 | 20)\ d {2}
Results: [1988]-11-13
Analysis: in this example, to exclude meaningless years, limit the first two digits of the year to 19 or 20, which is the OR operator in the regular expression. You must put 19 | 20 in a subexpression, that is, (19 | 20), otherwise it can only match to a year that begins with 20.
3. Nesting of subexpressions
Subexpressions allow nesting, and multiple layers of nesting are allowed, and the nesting hierarchy is theoretically unlimited.
In the expression (A) (B (C)), there are several subexpressions:
1 ((A) (B (C)
2 (A)
3 (B (C))
4 (C)
There are 4, and the 0th always represents the entire expression. Referencing subexpressions through\ n (n is the number of subexpressions) is described in later backtracking references.
For an example of subexpression nesting, see the regular expression that matches the IPV4 address below.
Thank you for reading! This is the end of this article on "how to use regular expressions". I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can 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.