Skip to content

Instantly share code, notes, and snippets.

@Istar-Eldritch
Last active August 29, 2015 14:26
Show Gist options
  • Save Istar-Eldritch/c1b909e1ffeef67014b6 to your computer and use it in GitHub Desktop.
Save Istar-Eldritch/c1b909e1ffeef67014b6 to your computer and use it in GitHub Desktop.
//
// 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