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:
1 |
let result = name.rangeOfString("^[a-z]", options: .RegularExpressionSearch) |
Direct will be able to get the captured content。
Gets to be a Range ,Tie strings as the corresponding methods,Nature can get captured content。If there is no,Returns an empty,Tie if Optional value binding,Fast and easy!
Original article written by LogStudio:R0uter's Blog » Swift in regular expressions
Reproduced Please keep the source and description link:https://www.logcg.com/archives/1948.html