swift 中使用正则表达式
2017-04-20 本文已影响36人
wylei
func checkNumber(str: String) -> Bool {
do {
let pattern = "^[0-9]*$"
let regex = try NSRegularExpression(pattern: pattern, options: NSRegularExpressionOptions.CaseInsensitive)
let res = regex.matchesInString(str, options: NSMatchingOptions(rawValue: 0), range: NSMakeRange(0, str.characters.count))
if res.count > 0 {
return true
}
return false
}
catch {
print(error)
return false
}
}