Created
May 4, 2017 19:29
-
-
Save CodingItWrong/a0abda808ca84db2f7e97daa05173fa1 to your computer and use it in GitHub Desktop.
NSRegularExpression+ActuallyReasonableMatch.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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