Grouping usage scenarios
When writing regular expressions, we usually have two scenarios that use grouping.
First, repeat a subexpression; second, you want to get the content matched by the subexpression.
- Repeat subexpressions
If you need to repeat a single character, just add a qualifier after the character, for examplea+
It means that one or more a matches,a?
Indicates that 0 or 1 a match.
But if we want to repeat multiple characters, we need to useGrouping 。
for example:(ab){3}
Repeat the ab character 3 times
Commonly used qualifiers in regularity are as follows:
expression | illustrate |
---|---|
X ? | X, once or once, no |
X * | X, zero or multiple times |
X + | X, once or more |
X { n } | X, exactly n times |
X { n ,} | X, at least n times |
X { n , m } | X , at least n times, but not more than m times |
- Get the content matched by the subexpression
For example, expression:[a-z]*\d*[a-z]*
, It means that the characters of a-z are repeated 0 to multiple times, followed by 0 to multiple numbers, followed by multiple characters of a-z.
Apparently, stringsabcd324232efg
is a string that satisfies the matching. Then, if we just want to get the number in the matching string324232
The string behindefg
Woolen cloth?
At this time, the regular expression can be rewritten by grouping:[a-z]*\d*([a-z]*)
. In this way, we can achieve our goal by getting the content matched by the 1st group.
How to use grouping
Pass brackets in the regular“()”
To specify the subexpression that needs to be repeated, and then add a qualifier to repeat the subexpression.
For example: (abc)? means 0 or 1 abc.
An expression in a set of brackets represents a group.
Capture Group
Grouping can be divided into two forms.Capture GroupandNon-capturing group。
The difference between a capture group and a non-capture group is that the group represented by the capture group captures text (i.e., match characters), while the group represented by the non-capture group does not capture text.
Capture groups can be numbered by calculating their open brackets from left to right 。
For example, in the expression (A)(B(C)), there are four such groups:
Grouping number | Subexpression corresponding to grouping number |
---|---|
0 | (A)(B(C)) |
1 | (A) |
2 | (B(C)) |
3 | (C) |
Note: The 0th grouping always represents the entire expression
The group's sequence number can be used in expressions through Back references (back references), or the content matching to the group can be retrieved from the matcher after the matching operation is completed.
The knowledge of backcitation will be analyzed in subsequent articles.
Examples of grouping
In a complete rule, if we only want to get the content matched by a certain subexpression, we can achieve our goal by grouping.
for example:
To be matched string:abcd324232efg
Want to get the second consecutive letter and child string in this stringefg
We can write rules in groups:[a-z]*\d*([a-z]*)
。
As you can see, we pass the subexpression([a-z]*)
to match the second consecutive mother's child string, and through()
Grouping has been added so that we can get the corresponding matching content through grouping.
The specific acquisition method may vary in the grammar of different languages, but the principles are the same.
Let’s take a look at how Javascript and Java are processed?
javascript get grouped content
var str = "abcd324232efg"; var reg = new RegExp("([a-z]*)(\\d*)([a-z]*)"); var arr = (reg); // Show matching group contentalert(arr[0] + "===" + arr[1] + "===" + arr[2] + "===" + arr[3]); alert(RegExp.$1 + "-----" + RegExp.$2 + "----" + RegExp.$3);
In the example above, I added 3 groups.
passarr[n]
andRegExp.$n
The group matching content can be obtained in all ways.
In javascript
\d
Need to escape
java get grouped content
public static void main(String[] args) { String str = "abcd324232efg"; Pattern pattern = ("([a-z]*)(\\d*)([a-z]*)"); Matcher matcher = (str); if (()) { (()); ((0)); ((1)); ((2)); ((3)); } }
In java, by(n)
get group matching content in a way.
In javascript
\d
Need to escape
summary
There are usually two usage scenarios for grouping: one is to repeat a subexpression; the other is to obtain the content matched by the subexpression.
Grouping is through()
to represent it, it is numbered by calculating its open brackets from left to right.
Supplement: Regular expression grouping and common methods
/* * Regular expression grouping function: * Capture groups can be numbered by calculating their open brackets from left to right. For example, in the expression ((A)(B(C)))), there are four such groups: * split(regex) parameter is a regular expression The return value is an array * replaceAll(regex,replacement) The first parameter is a regular expression, and the second parameter is to be replaced with a string */ import ; public class RegexApply { public static void main(String[] args) { // TODO Auto-generated method stub // demo1(); // demo2(); // demo3(); // demo4(); // demo5(); // demo6(); } private static void demo6() { /* * \\. "" Replace all . with empty strings * (.)\\1+ "$1" replaces the first group of occurrences multiple times. The first group of occurrences $ represents the first character */ String s = "I...I...I...I...I...I...I...I...I...I...I...I...I...I'll love...I'll love...I'll learn...I'll learn...I'll learn."; String s2 = ("\\.+", ""); String s3 = ("(.)\\1+", "$1"); (s3); } private static void demo5() { /* * Repeating word cutting: "acyyfgttthjzzzzzzko"; * (.)\\1+ //1+ means that the first group appears once to multiple times */ String s = "acyyfgttthjzzzzzko"; String regex = "(.)\\1+"; //1+ means that the first group appears once to multiple times String[] arr = (regex); for (int i = 0; i < ; i++) { (arr[i]); } } private static void demo4() { /* * Word repellent Capture group (.)\\1(.)\\2\\1 means that the first group appears again \\2 means that the second group appears again * (..)\\1 \\1 means that the first group appears again */ //Repeat words: fluttering, beautiful, beautiful String regex2 = "(.)\\1(.)\\2"; //\\1 means that the first group appears again \\2 means that the second group appears again ("Beautiful".matches(regex2)); //true ("Beautiful and Beautiful".matches(regex2)); //false ("Happy".matches(regex2)); //true ("Diet, die".matches(regex2)); //false ("----------------------"); //Repeat words beautiful, beautiful, beautiful String regex = "(..)\\1"; ("Diet, die".matches(regex)); //true ("Happy".matches(regex)); //true ("Happy".matches(regex)); //false } private static void demo3() { /* * replaceAll(regex,replacement) The first parameter is a regular expression, and the second parameter is to be replaced with a string */ String s = "aaoo1ddd3jgjao"; String regex = "\\d"; String s2 = (regex, ""); String s3 = (regex, ""); (s2); //aaooddd3jgjao (s3); //aaoodddjgjao } private static void demo2() { /* * split(regex) parameter is a regular expression The return value is an array */ String s = "Monday.Tuesday.Thai.Thursday"; String[] array = ("\\."); ((array)); } private static void demo1() { /* * Check the phone number * 1. Must be 5-15 digits * 2. The beginning cannot be 0 * 3. Must be pure numbers */ String regex = "[1-9]\\d{5,15}"; ("804360385".matches(regex)); //true ("430763075439703307503".matches(regex)); //false ("03534534".matches(regex)); //false } }
Summarize
This is the article about grouping and use of advanced usage of regular expressions. For more related contents of grouping of regular expressions, please search for my previous articles or continue browsing the related articles below. I hope you will support me in the future!