Skip to content

Instantly share code, notes, and snippets.

@mattt
Created December 29, 2014 22:35
Show Gist options
  • Star 37 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mattt/3f12f56d72b8d2ebbe62 to your computer and use it in GitHub Desktop.
Save mattt/3f12f56d72b8d2ebbe62 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
}
}
@MatrixSenpai
Copy link

Made redundant with the introduction of the following for Strings

String.rangeOfString(contains: String)

Use the following instead

extension String {
    func contains(find t: String) -> Bool {
       return self.rangeOfString(t) != nil
   }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment