Bool { let matches = self.getMatchesWithRegex(regex) return matches.count > 0 } }"> Bool { let matches = self.getMatchesWithRegex(regex) return matches.count > 0 } }"> Bool { let matches = self.getMatchesWithRegex(regex) return matches.count > 0 } }">
extension String {
func getMatchesWithRegex(_ regex: String) -> [String] {
do {
let regex = try NSRegularExpression(pattern: regex)
let results = regex.matches(in: self, range: NSRange(self.startIndex..., in: self))
return results.map({
String(self[Range($0.range, in: self)!])
})
} catch {
fatalError("regex 문자가 잘못되었습니다. \\(regex)")
}
}
func foundMatchesWithRegex(_ regex: String) -> Bool {
let matches = self.getMatchesWithRegex(regex)
return matches.count > 0
}
}