Forked from CodingItWrong/NSRegularExpression+ActuallyReasonableMatch.swift
Created
July 18, 2017 05:03
-
-
Save ghecho/6246a27a9f155104dd55f2c5db77f7d5 to your computer and use it in GitHub Desktop.
NSRegularExpression+ActuallyReasonableMatch.swift
This file contains 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