Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save CodingItWrong/a0abda808ca84db2f7e97daa05173fa1 to your computer and use it in GitHub Desktop.
Save CodingItWrong/a0abda808ca84db2f7e97daa05173fa1 to your computer and use it in GitHub Desktop.
NSRegularExpression+ActuallyReasonableMatch.swift
extension NSRegularExpression {
func actuallyUsableMatch(in string: String) -> (fullMatch: String, captures: [String])? {
let nsString = string as NSString
let range = NSMakeRange(0, nsString.length)
guard let match = firstMatch(in: string, range: range) else {
return nil
}
let fullMatch = nsString.substring(with: match.range)
var captures: [String] = []
for i in 1 ..< match.numberOfRanges {
captures.append(nsString.substring(with: match.rangeAt(i)))
}
return (fullMatch, captures)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment