I have a regex search method in string:
extension String {
func searchRegex (regex: String) -> Array {
do {
let rege
NSTextCheckingResult
has a numberOfRanges
property and a rangeAtIndex()
method that lets you grab the range for individual capture groups. So if you wanted the first capture group instead of the whole matched string, you would modify your code to:
var matches : Array<String> = Array<String>()
regex.enumerateMatchesInString(self, options: NSMatchingOptions(rawValue: 0), range: all) {(result : NSTextCheckingResult?, _, _) in
let capturedRange = result!.rangeAtIndex(1)
if !NSEqualRanges(capturedRange, NSMakeRange(NSNotFound, 0)) {
let theResult = nsstr.substringWithRange(result!.rangeAtIndex(1))
matches.append(theResult)
}
}