Help
Import Java.util. * ;
Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern;
public class Fenzhu
... {
public static void Main (string[] args)
... {
Pattern p = pattern.compile ("(/d{3,5}) ([a-z]{2})");
String s = "123aa-34345bb-234cc-00";
Matcher m = P.matcher (s);
while (M.find ())
... {
System.out.println ("M.group ():" +m.group ()); Print all
System.out.println ("M.group (1):" +m.group (1)); Print the number of
System.out.println ("M.group (2):" +m.group (2)); Print the letter
System.out.println ();
}
System.out.println ("Number of captures: groupcount () =" +m.groupcount ());
}
}
Output results are
Appendix: Description Group for help documentation
Group (int group)
Returns the input subsequence captured by a given group during a previous matching operation.
For the match m, the input sequence s and the group index G, the expression m.group (g) and S.substring (M.start (g), M.end (g)) are equivalent.
The capturing group is the index from left to right starting at 1. group 0 represents the entire pattern, so an expression m.group (0) is equivalent to M.group ().
If the match succeeds, but the specified group fails to match any part of the input sequence, NULL is returned. Note that some groups (for example, (A *)) match an empty string. This method returns an empty string when these groups successfully match an empty string in the input.
specified by: group parameter in interface Matchresult : Group-The index of the capturing group in this matching pattern. returns: a sequence of subgroups (possibly empty) captured by the group during the previous match, or null if the group could not match the part of the input. Thrown: IllegalStateException-If no match was attempted, or if a previous match failed. Indexoutofboundsexception-If no capture group exists in the schema of the given index. GroupCount
GroupCount ()
returns the number of capturing groups in this matching pattern.According to convention, 0 groups represent the entire pattern. It is not included in this count.
Any non-negative integer less than or equal to the return value of this method is guaranteed to be a valid group index for this match.
specified by: GroupCount in interface matchresult returns: the number of capturing groups in this matching pattern.