You can treat characters as a unit by using them as a group.

A regular expression group is marked by parentheses.

For example, (ab), ab(z), ab(ab)(xyz), (the((is)(is))).

Each group in a regular expression has a group number which starts at 1.

Matcher class groupCount() method returns the number of groups in the pattern.

group 0 refers to the entire regular expression, which is not reported by the groupCount() method.

Each left parenthesis inside a regular expression marks the start of a new group.

For Regular Expression AB(XY)

Group Number | Group Text |
---|---|

0 | AB(XY) |

1 | (XY) |

For Regular Expression: (AB)(XY)

Group Number | Group Text |
---|---|

0 | (AB)(XY) |

1 | (AB) |

2 | (XY) |

For Regular Expression: ((A)((X)(Y)))

Group Number | Group Text |
---|---|

0 | ((A)((X)(Y))) |

1 | ((A)((X)(Y))) |

2 | (A) |

3 | ((X)(Y)) |

4 | (X) |

5 | (Y) |

For Regular Expression: ABCD

Group Number | Group Text |
---|---|

0 | ABCD |

The following code shows how to use Groups in Regular Expressions.

import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String[] args) { // Prepare a regular expression: A group of 3 digits followed by 7 digits.Stringregex = "\\b(\\d{3})\\d{7}\\b"; // Compile the regular expressionPatternp =Pattern.compile(regex);Stringsource = "1234567890, 1234567, and 1234567890"; // Get the Matcher objectMatcherm = p.matcher(source); // Start matching and display the found area codes while (m.find()) {Stringphone = m.group();/*from w w w.j a va 2 s. c om*/StringareaCode = m.group(1);System.out.println("Phone: " + phone + ", Area Code: " + areaCode); } } }

- Use Group to format or replace the matched string with another string.
- Back Referencing a Group in a Replacement Text
- Regular Expression Named Groups
- Match boundary for groups