Skip to content

Instantly share code, notes, and snippets.

@algal
Created May 9, 2016 20:47
Show Gist options
  • Save algal/8225f9d70f4a725d25a758357de3b628 to your computer and use it in GitHub Desktop.
Save algal/8225f9d70f4a725d25a758357de3b628 to your computer and use it in GitHub Desktop.
// known-good: Swift 2.2, Xcode 7.3
struct Regex {
let matcher:NSRegularExpression
init?(pattern: String, options: NSRegularExpressionOptions = []) {
guard let m = try? NSRegularExpression(pattern: pattern, options: options) else { return nil }
self.matcher = m
}
func match(string: String, options: NSMatchingOptions = []) -> Bool {
let stringRange = NSMakeRange(0, (string as NSString).length)
let matches = self.matcher.numberOfMatchesInString(string,
options: options,
range: stringRange)
return matches != 0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment