I have a string and an NSRegularExpression
instance:
let string = "example string"
let regex = try! NSRegularExpression(pattern: #"(example) (string)"#)
When I try to find the first match using firstMatch(in:options:range:)
, how do I get the range
parameter correctly?
let match = regex.firstMatch(
in: string,
range: NSRange(location: 0, length: string.count)
)
// or
let match = regex.firstMatch(
in: string,
range: NSRange(location: 0, length: string.lengthOfBytes(using: .utf8))
)
// or
let match = regex.firstMatch(
in: string,
range: NSRange(location: 0, length: string.lengthOfBytes(using: .utf16))
)
I can't find much of any useful documentation on NSRange
, other than that it "describe a portion of a series, such as characters in a string".