在一開始,Swift 裡並沒有提供正則表達式的支持,所以我們只能自己來封裝,比如說寫一個結構體,像這樣:
| 
					 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         }     } }  | 
					
其實現在 Swift 已經有了對正則表達式的支持,我們只需要這樣:
[crayon-690942feefaab281506[……]