<!--
{
  "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/init(_:as:)-2ucu7",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:17_StringProcessing5RegexV_2asACyxGSgACyAA03AnyC6OutputVG_xmtcfc"
  },
  "title" : "init(_:as:)"
}
-->

# init(_:as:)

Creates a regular expression with a strongly-typed capture list from the
given regular expression.

```
init?(_ regex: Regex<AnyRegexOutput>, as outputType: Output.Type = Output.self)
```

## Parameters

`regex`

A regular expression to convert to use a strongly-typed capture
list.

`outputType`

The capture structure to use.

## Discussion

You can use this initializer to convert a regular expression with a
dynamic capture list to one with a strongly-typed capture list. If the
type you provide as `outputType` doesn’t match the capture structure of
`regex`, the initializer returns `nil`.

```
let dynamicRegex = try Regex("(.+?): (.+)")
if let stronglyTypedRegex = Regex(dynamicRegex, as: (Substring, Substring, Substring).self) {
    print("Converted properly")
}
// Prints "Converted properly"
```

---

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)