<!--
{
  "availability" : [
    "iOS: 16.0.0 -",
    "iPadOS: 16.0.0 -",
    "macCatalyst: 16.0.0 -",
    "macOS: 13.0.0 -",
    "tvOS: 16.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 9.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/Regex/wholeMatch(in:)-9do8t",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:17_StringProcessing5RegexV10wholeMatch2inAC0E0Vyx_GSgSS_tKF"
  },
  "title" : "wholeMatch(in:)"
}
-->

# wholeMatch(in:)

Returns a match if this regex matches the given string in its entirety.

```
func wholeMatch(in string: String) throws -> Regex<Output>.Match?
```

## Parameters

`string`

The string to match this regular expression against.

## Return Value

The match, if this regex matches the entirety of `string`;
otherwise, `nil`.

## Discussion

Call this method if you want the regular expression to succeed only when
it matches the entire string you pass as `string`. The following example
shows matching a regular expression that only matches digits, with
different candidate strings.

```
let digits = /[0-9]+/

if let digitsMatch = try digits.wholeMatch(in: "2022") {
    print(digitsMatch.0)
} else {
    print("No match.")
}
// Prints "2022"

if let digitsMatch = try digits.wholeMatch(in: "The year is 2022.") {
    print(digitsMatch.0)
} else {
    print("No match.")
}
// Prints "No match."
```

The `wholeMatch(in:)` method can throw an error if this regex includes
a transformation closure that throws an error.

---

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)