Skip to content

Instantly share code, notes, and snippets.

@vinhnx
Forked from mattt/Regex.swift
Created October 8, 2016 01:59
Show Gist options
  • Save vinhnx/6106fb9839d54b9fedc537966e5522c2 to your computer and use it in GitHub Desktop.
Save vinhnx/6106fb9839d54b9fedc537966e5522c2 to your computer and use it in GitHub Desktop.
struct Regex {
let pattern: String
let options: NSRegularExpressionOptions!
private var matcher: NSRegularExpression {
return NSRegularExpression(pattern: self.pattern, options: self.options, error: nil)
}
init(pattern: String, options: NSRegularExpressionOptions = nil) {
self.pattern = pattern
self.options = options
}
func match(string: String, options: NSMatchingOptions = nil) -> Bool {
return self.matcher.numberOfMatchesInString(string, options: options, range: NSMakeRange(0, string.utf16Count)) != 0
}
}
extension Regex: StringLiteralConvertible {
typealias ExtendedGraphemeClusterLiteralType = StringLiteralType
init(unicodeScalarLiteral value: UnicodeScalarLiteralType) {
self.pattern = "\(value)"
}
init(extendedGraphemeClusterLiteral value: ExtendedGraphemeClusterLiteralType) {
self.pattern = value
}
init(stringLiteral value: StringLiteralType) {
self.pattern = value
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment