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

# init(_:)

Creates a regular expression from the given string, using a dynamic
capture list.

```
init(_ pattern: String) throws
```

## Parameters

`pattern`

A string with regular expression syntax.

## Discussion

Use this initializer to create a `Regex` instance from a regular
expression that you have stored in `pattern`.

```
let simpleDigits = try Regex("[0-9]+")
```

This initializer throws an error if `pattern` uses invalid regular
expression syntax.

The output type of the new `Regex` is the dynamic [`AnyRegexOutput`](/documentation/Swift/AnyRegexOutput).
If you know the capture structure of `pattern` ahead of time, use the
`init(_:as:)` initializer instead.

---

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)