init(_:as:)
Creates a regular expression with a strongly-typed capture list from the given regular expression.
Declaration
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"