-
Notifications
You must be signed in to change notification settings - Fork 0
/
String+Regex.swift
28 lines (23 loc) · 898 Bytes
/
String+Regex.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import Foundation
public enum Regex: String {
case email = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}"
case number = "^[0-9]+$"
case fullNames = "^[a-zA-Z\\s]*$" // fullname
case validPassword = "^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{9,}$"
case haveAtleaseOneSpecialSymbol = "^(?=.*?[#?!@$%^&*-])"
case atleaseOneNumber = "[0-9]"
case containsEightDigits = ".{8,}"
var pattern: String {
return rawValue
}
}
public extension String {
public func match(_ pattern: String) -> Bool {
do {
let regex = try NSRegularExpression(pattern: pattern, options: [.caseInsensitive])
return regex.firstMatch(in: self, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: NSRange(location: 0, length: count)) != nil
} catch {
return false
}
}
}