---
schema_version: "1"
id: 2018f3db92e1df15c33530c65c3584009fba9c688a85e6a7f89e166ad95fbf5a
title: Understanding Capturing Groups
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/apexcode/apex_classes_pattern_and_matcher_capturing_groups.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_pattern_and_matcher_capturing_groups.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: d5f73b08bde70f8ad2bcd95e2debba3c9688c733ff01cb781557651e8bb8e38f
---
# Understanding Capturing Groups

During a matching operation, each substring of the input string that matches the pattern is saved. These matching substrings are called capturing groups.

Capturing groups are numbered by counting their opening parentheses from left to right. For example, in the regular expression string ((A)(B(C))), there are four capturing groups:

1. ((A)(B(C)))
2. (A)
3. (B(C))
4. (C)

Group zero always stands for the entire expression.

The captured input associated with a group is always the substring of the group most recently matched, that is, that was returned by one of the Matcher class match operations.

If a group is evaluated a second time using one of the match operations, its previously captured value, if any, is retained if the second evaluation fails.