Last active
August 29, 2015 14:26
-
-
Save Istar-Eldritch/c1b909e1ffeef67014b6 to your computer and use it in GitHub Desktop.
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
// | |
// Regex.swift | |
// | |
// Created by Ruben Paz | |
// | |
import Foundation | |
class Regex { | |
let internalExpression: NSRegularExpression | |
let pattern: String | |
init(_ pattern: String) { | |
self.pattern = pattern | |
var error: NSError? | |
self.internalExpression = NSRegularExpression(pattern: pattern, options: nil, error: &error)! | |
} | |
func match(input: String) -> [String] { | |
let m = self.internalExpression.matchesInString(input, options: nil, range: NSMakeRange(0, count(input))).first as? NSTextCheckingResult | |
func getGroup( index: Int) -> String { | |
return (input as NSString).substringWithRange(m!.rangeAtIndex(index)) | |
} | |
let groups = (m?.numberOfRanges - 1) ?? 0 | |
let result = (0...(groups)).map(getGroup) | |
return result | |
} | |
} | |
extension String { | |
func r() -> Regex { | |
return Regex(self) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment