Skip to content

Instantly share code, notes, and snippets.

@eMdOS
Last active March 15, 2017 00:53
Show Gist options
  • Save eMdOS/e32b6dade644836166d31c6794db2aa8 to your computer and use it in GitHub Desktop.
Save eMdOS/e32b6dade644836166d31c6794db2aa8 to your computer and use it in GitHub Desktop.
Regex
public enum Regex: String {
case email = "^[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$"
}
extension Regex: RegexProvider {
public var regex: String {
return rawValue
}
static func validate(expression: String, forRegex regex: Regex) -> Bool {
return validate(expression: expression, forRegexString: regex.regex)
}
}
public protocol RegexProvider {
var regex: String { get }
}
public extension RegexProvider {
static func validate(expression: String, forRegex regex: RegexProvider) -> Bool {
return validate(expression: expression, forRegexString: regex.regex)
}
static func validate(expression: String, forRegexString string: String) -> Bool {
return NSPredicate(format: "SELF MATCHES %@", string).evaluate(with: expression)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment