<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.0.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSRegularExpression/enumerateMatches(in:options:range:using:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSRegularExpression(im)enumerateMatchesInString:options:range:usingBlock:"
  },
  "title" : "enumerateMatches(in:options:range:using:)"
}
-->

# enumerateMatches(in:options:range:using:)

Enumerates the string allowing the Block to handle each regular expression match.

```
func enumerateMatches(in string: String, options: NSRegularExpression.MatchingOptions = [], range: NSRange, using block: (NSTextCheckingResult?, NSRegularExpression.MatchingFlags, UnsafeMutablePointer<ObjCBool>) -> Void)
```

## Parameters

`string`

The string.

`options`

The matching options to report. See [`NSRegularExpression.MatchingOptions`](/documentation/Foundation/NSRegularExpression/MatchingOptions) for the supported values.

`range`

The range of the string to test.

`block`

The Block enumerates the matches of the regular expression in the string.

    The block takes three arguments:

- result: An [`NSTextCheckingResult`](/documentation/Foundation/NSTextCheckingResult) specifying the match. This result gives the overall matched range via its [`range`](/documentation/Foundation/NSTextCheckingResult/range) property, and the range of each individual capture group via its [`range(at:)`](/documentation/Foundation/NSTextCheckingResult/range(at:)) method. The range {`NSNotFound`, 0} is returned if one of the capture groups did not participate in this particular match.
- flags: The current state of the matching progress. See [`NSRegularExpression.MatchingFlags`](/documentation/Foundation/NSRegularExpression/MatchingFlags) for the possible values.
- stop: A reference to a Boolean value. The Block can set the value to <doc://com.apple.documentation/documentation/Swift/true> to stop further processing of the array. The stop argument is an out-only argument. You should only ever set this Boolean to <doc://com.apple.documentation/documentation/Swift/true> within the Block.

    The Block returns void.

## Discussion

This method is the fundamental matching method for regular expressions and is suitable for overriding by subclassers. There are additional convenience methods for returning all the matches as an array, the total number of matches, the first match, and the range of the first match.

By default, the Block iterator method calls the Block precisely once for each match, with a non-`nil` `result` and the appropriate `flags`.  The client may then stop the operation by setting the contents of `stop` to <doc://com.apple.documentation/documentation/Swift/true>. The `stop` argument is an out-only argument. You should only ever set this Boolean to <doc://com.apple.documentation/documentation/Swift/true> within the Block.

If the [`reportProgress`](/documentation/Foundation/NSRegularExpression/MatchingOptions/reportProgress) matching option is specified, the Block will also be called periodically during long-running match operations, with `nil` result and [`progress`](/documentation/Foundation/NSRegularExpression/MatchingFlags/progress) matching flag set in the Block’s `flags` parameter, at which point the client may again stop the operation by setting the contents of stop to <doc://com.apple.documentation/documentation/Swift/true>.

If the [`reportCompletion`](/documentation/Foundation/NSRegularExpression/MatchingOptions/reportCompletion) matching option is specified, the Block object will be called once after matching is complete, with `nil` result and the [`completed`](/documentation/Foundation/NSRegularExpression/MatchingFlags/completed) matching flag is set in the `flags` passed to the Block, plus any additional relevant [`NSRegularExpression.MatchingFlags`](/documentation/Foundation/NSRegularExpression/MatchingFlags) from among [`hitEnd`](/documentation/Foundation/NSRegularExpression/MatchingFlags/hitEnd), [`requiredEnd`](/documentation/Foundation/NSRegularExpression/MatchingFlags/requiredEnd), or [`internalError`](/documentation/Foundation/NSRegularExpression/MatchingFlags/internalError).

[`progress`](/documentation/Foundation/NSRegularExpression/MatchingFlags/progress) and [`completed`](/documentation/Foundation/NSRegularExpression/MatchingFlags/completed) matching flags have no effect for methods other than this method.

The [`hitEnd`](/documentation/Foundation/NSRegularExpression/MatchingFlags/hitEnd) matching flag is set in the `flags` passed to the Block if the current match operation reached the end of the search range.  The [`requiredEnd`](/documentation/Foundation/NSRegularExpression/MatchingFlags/requiredEnd) matching flag is set in the `flags` passed to the Block if the current match depended on the location of the end of the search range.

The [`NSRegularExpression.MatchingFlags`](/documentation/Foundation/NSRegularExpression/MatchingFlags) matching flag is set in the `flags` passed to the block if matching failed due to an internal error (such as an expression requiring exponential memory allocations) without examining the entire search range.

The [`anchored`](/documentation/Foundation/NSRegularExpression/MatchingOptions/anchored), [`withTransparentBounds`](/documentation/Foundation/NSRegularExpression/MatchingOptions/withTransparentBounds), and [`withoutAnchoringBounds`](/documentation/Foundation/NSRegularExpression/MatchingOptions/withoutAnchoringBounds) regular expression options, specified in the [`options`](/documentation/Foundation/NSRegularExpression/options-swift.property) property specified when the regular expression instance is created, can apply to any match or replace method.

If [`anchored`](/documentation/Foundation/NSRegularExpression/MatchingOptions/anchored) matching option is specified, matches are limited to those at the start of the search range.

If [`withTransparentBounds`](/documentation/Foundation/NSRegularExpression/MatchingOptions/withTransparentBounds) matching option is specified, matching may examine parts of the string beyond the bounds of the search range, for purposes such as word boundary detection, lookahead, etc.

If [`withoutAnchoringBounds`](/documentation/Foundation/NSRegularExpression/MatchingOptions/withoutAnchoringBounds) matching option is specified, `^` and `$` will not automatically match the beginning and end of the search range, but will still match the beginning and end of the entire string.

[`withTransparentBounds`](/documentation/Foundation/NSRegularExpression/MatchingOptions/withTransparentBounds) and [`withoutAnchoringBounds`](/documentation/Foundation/NSRegularExpression/MatchingOptions/withoutAnchoringBounds) matching options have no effect if the search range covers the entire string.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)