---
title: numberOfCaptureGroups
framework: foundation
role: symbol
role_heading: Instance Property
path: foundation/nsregularexpression/numberofcapturegroups
---

# numberOfCaptureGroups

Returns the number of capture groups in the regular expression.

## Declaration

```swift
var numberOfCaptureGroups: Int { get }
```

## Discussion

Discussion A capture group consists of each possible match within a regular expression. Each capture group can then be used in a replacement template to insert that value into a replacement string. This value puts a limit on the values of n for $n in templates, and it determines the number of ranges in the returned NSTextCheckingResult instances returned in the match... methods. An exception will be generated if you attempt to access a result with an index value exceeding numberOfCaptureGroups``-1.

## See Also

### Getting the Regular Expression and Options

- [pattern](foundation/nsregularexpression/pattern.md)
- [options](foundation/nsregularexpression/options-swift.property.md)
