很久不用正则就忘了怎么用了。。赶项目今天还没空学,找了一个正则网站,还不错
http://tools.jb51.net/regex/create_reg
swift3.0代码
private func checkFloatNum(str: String) -> Bool {
// 使用正则表达式一定要加try语句
do {
// - 1、创建规则
let pattern = "[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*"
// - 2、创建正则表达式对象
let regex = try NSRegularExpression(pattern: pattern, options: NSRegularExpression.Options.caseInsensitive)
// - 3、开始匹配
let res = regex.matches(in: str, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: NSMakeRange(0, str.characters.count))
// 输出结果
guard res.count > 0 else {
SVProgressHUD.showError(withStatus: "经纬度只能输入数字")
return false
}
for checkingRes in res {
print((str as NSString).substring(with: checkingRes.range))
return true
}
}
catch {
print(error)
return false
}
return false
}