Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Playground - noun: a place where people can play
import Cocoa
struct Regex {
let pattern: String
let expressionOptions: NSRegularExpressionOptions
let matchingOptions: NSMatchingOptions
init(pattern: String, expressionOptions: NSRegularExpressionOptions, matchingOptions: NSMatchingOptions) {
self.pattern = pattern
self.expressionOptions = expressionOptions
self.matchingOptions = matchingOptions
}
init(pattern:String) {
self.pattern = pattern
expressionOptions = NSRegularExpressionOptions(0)
matchingOptions = NSMatchingOptions(0)
}
}
let r1 = Regex(pattern: "[a-z]")
let r2 = Regex(pattern: "[0-9]", expressionOptions: NSRegularExpressionOptions(0), matchingOptions: NSMatchingOptions(0))
operator infix =~ { associativity left precedence 140 }
func =~(left: String, right: Regex) -> Bool {
let range: NSRange = NSMakeRange(0, countElements(left))
if let regex = NSRegularExpression.regularExpressionWithPattern(right.pattern, options: right.expressionOptions, error: nil) {
let matches: AnyObject[] = regex.matchesInString(left, options: right.matchingOptions, range: range)
return matches.count > 0
} else {
"wat"
}
return false
}
func =~(left: String, right: String) -> Bool {
return left =~ Regex(pattern: right)
}
"a" =~ r1
"2" =~ r1
"a" =~ r2
"2" =~ r2
"2" =~ "[a-z]"
"2" =~ "[0-9]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.