To figure out these three methods, you first need to understand the concept of capturing groups in a Java regular expression. The capturing group is also the sub-pattern of the pattern in parentheses to "()". The main reason to use capturing groups is to find out what you care more about in a match.
Capturing groups can be numbered by calculating their opening brackets from left to right. For example, in an expression "(x) (y\\w*) (z)", there are three such groups:
1. x
2. y\\w*
3. Z
Group 0 always represents an entire expression.
This is why the capturing group is named because in the match, each subsequence of the input sequence that matches these groups is saved. The captured subsequence can later be used in an expression through the back reference, or it can be obtained from the match after the match operation is complete.
A group that begins with a (?) is a pure, non-capturing group that does not capture text and does not count against group totals.
Example:
Package pattern;
Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern;
public class Testregex {
public static void Main (string[] args) {
String regex = "(x) (y\\w*) (z)";
String input = "exy123z,xy456z";
Pattern p = pattern.compile (regex, pattern.case_insensitive);
Matcher m = p.matcher (input);
while (M.find ()) {
System.out.println (M.group (2));
}
}
}
Operation Result:
Y123
y456
http://blog.163.com/[email protected]/blog/static/1662603142011219625597/