Java Regular expression: What is a grouping group ()

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.