swift 中使用正则表达式

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
    }
}

推荐阅读更多精彩内容