我正在编写一个代码,它接受一个字符串并对其进行验证。首先,我们创建了一个字符串长度为5或5以上且小于24的正则表达式,并且只能使用大小写字母和破折号,然后首先进行验证。
func iDValidator(_ id: String) -> Bool {
let lengthAndElementRegEx = "^\w{5,23}[a-zA-Z0-9-]$"
let continuousRegEx = "I Need!!!"
let lengthAndElementValidate = NSPredicate(format:"SELF MATCHES %@", lengthAndElementRegEx)
guard lengthAndElementValidate.evaluate(with: id) == true else {
return false
}
let continuousValidate = NSPredicate(format:"SELF MATCHES %@", continuousRegEx)
guard continuousValidate.evaluate(with: id) == true else {
print("###false")
return false
}
return true
}
之后,我想验证一个数字在一行中出现3次,或者在一个字符串中出现3个连续的数字,例如"0";111〃"222〃"123〃;,以及";456〃;。你有一个简单的正则表达式吗?帮助
您可以使用这样的东西:
([0-9])1{2}|(012|123|234|345|456|567|678|789)
其中:
([0-9])
-第1组捕获1位1{2}
-捕获组1中捕获的值的2次以上(由于限制量词{2}
((其中1
是组1值的反向引用((012|123|234|345|456|567|678|789)
-简单地匹配所有可能的数字序列
您可以自己尝试:https://regex101.com/r/noBKsE/2
源