At the beginning of a,Swift did not provide regular expression support,So we can only package,For example write a structure,Like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
struct MyRegex { let regex: NSRegularExpression? init(_ pattern: String) { regex = try? NSRegularExpression(pattern: pattern, options: .CaseInsensitive) } func match(input: String) -> Bool { if let matches = regex?.matchesInString(input, options: [], range: NSMakeRange(0, (input as NSString).length)) { return matches.count > 0 } else { return false } } } |
In fact, Swift already has support for regular expressions,We just need to:
[crayon-67043bbb517e4051182[……]