<!--
{
  "availability" : [
    "iOS: 16.0.0 -",
    "iPadOS: 16.0.0 -",
    "macCatalyst: 16.0.0 -",
    "macOS: 13.0.0 -",
    "tvOS: 16.0.0 -",
    "visionOS: -",
    "watchOS: 9.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/RegexBuilder/ChoiceOf/init(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "RegexBuilder"
    ],
    "preciseIdentifier" : "s:12RegexBuilder8ChoiceOfVyACyxGADyXEcfc"
  },
  "title" : "init(_:)"
}
-->

# init(_:)

Creates a regex component that chooses exactly one of the regex components
provided by the builder closure.

```
init(@AlternationBuilder _ builder: () -> ChoiceOf<Output>)
```

## Parameters

`builder`

A builder closure that declares a list of regex
components, each of which can be exclusively matched.

## Discussion

In this example, `regex` successfully matches either a `"CREDIT"` or
`"DEBIT"` substring:

```
let regex = Regex {
    ChoiceOf {
        "CREDIT"
        "DEBIT"
    }
}
let match = try regex.prefixMatch(in: "DEBIT    04032020    Payroll $69.73")
print(match?.0 as Any)
// Prints "DEBIT"
```

---

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)