I'm having a problem with this function:
private func regexFirstGroup(in intext: String, pattern: String) -> String? {
do {
let nsrange = NSRange(intext.startIndex..<intext.endIndex, in: intext)
let regex = try NSRegularExpression(pattern: pattern, options: [NSRegularExpression.Options.dotMatchesLineSeparators])
if let match = regex.firstMatch(in: intext, options: [], range: nsrange) {
guard let mrange = Range(match.range(at: 1), in:intext) else {
return nil
}
return intext.substring(with: mrange)
}
} catch {
print("Pattern \(pattern) compiled with error: \(error)")
return nil
}
return nil
}
line with:
return intext.substring(with: mrange)
gives an deprecated warning: 'substring(with:)' is deprecated: Please use String slicing subscript.
and when I switch it to expected swift5 way:
return intext[mrange]
than it gives an error: Subscript 'subscript(_:)' requires the types 'String.Index' and 'Int' be equivalent
This is strange, because subscripting should work with String.Index types, and inspecting mrange constant in quick help shows:
Declaration
let mrange: Range<String.Index>
I'm not using import Foundation atm.
You should use subscript(_:)
return String(intext[mrange])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With