Last active
March 15, 2017 00:53
-
-
Save eMdOS/e32b6dade644836166d31c6794db2aa8 to your computer and use it in GitHub Desktop.
Regex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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