只需更新选择的Swift 3答案:
func matchesForRegexInText(regex: String!,text: String!) -> [String] { do { let regex = try NSRegularExpression(pattern: regex,options: []) let nsString = text as NSString let results = regex.matches(in: text,options: [],range: NSMakeRange(0,nsString.length)) return results.map { nsString.substring(with: $0.range)} } catch let error as NSError { print("invalid regex: \(error.localizedDescription)") return [] }}
用法保持不变.