---
title: "init(pattern:options:)"
framework: foundation
role: symbol
role_heading: Initializer
path: "foundation/nsregularexpression/init(pattern:options:)"
---

# init(pattern:options:)

Returns an initialized NSRegularExpression instance with the specified regular expression pattern and options.

## Declaration

```swift
init(pattern: String, options: NSRegularExpression.Options = []) throws
```

## Parameters

- `pattern`: The regular expression pattern to compile.
- `options`: The regular expression options that are applied to the expression during matching. See doc://com.apple.foundation/documentation/Foundation/NSRegularExpression/Options-swift.struct for possible values.

## Return Value

Return Value An instance of NSRegularExpression for the specified regular expression and options.

## Discussion

Discussion note: In Swift, this API is imported as an initializer and is marked with the throws keyword to indicate that it throws an error in cases of failure. You call this method in a try expression and handle any errors in the catch clauses of a do statement, as described in Error Handling in The Swift Programming Language and About Imported Cocoa Error Parameters.
