Getting captured subtext with is NSRegularExpressionnot so simple.
First of all, the result matches(in:range:)is equal [NSTextCheckingResult], and each NSTextCheckingResultdoes not match the type, for example (first?, second).
, NSTextCheckingResult rangeAt(_:). rangeAt(0) , , rangeAt(1) , rangeAt(2) ..
rangeAt(_:) NSRange, Swift Range. (location length) UTF-16 NSString.
, rangeAt(_:) NSRange(location: NSNotFound, length: 0) .
, - :
let text1 = "something with foo and bar"
let text2 = "something with just bar"
let regex = try! NSRegularExpression(pattern: "(?:(foo).*)?(bar)")
for match in regex.matches(in: text1, range: NSRange(0..<text1.utf16.count)) {
let firstRange = match.rangeAt(1)
let secondRange = match.rangeAt(2)
let first = firstRange.location != NSNotFound ? (text1 as NSString).substring(with: firstRange) : nil
let second = (text1 as NSString).substring(with: secondRange)
print(first)
print(second)
}
for match in regex.matches(in: text2, range: NSRange(0..<text2.utf16.count)) {
let firstRange = match.rangeAt(1)
let secondRange = match.rangeAt(2)
let first = firstRange.location != NSNotFound ? (text2 as NSString).substring(with: firstRange) : nil
let second = (text2 as NSString).substring(with: secondRange)
print(first)
print(second)
}