正则 电话号码 车牌号 邮政编码 价格 汉字 密码 特殊字符
刚搭建了一个项目框架,因为项目中有的地方需要用到正则,所以集成了一些常用的正则表达式,在这里和大家分享一下.
1. 验证电话号码
funcisPhoneNumber()->Bool{
let phone = "^((1[34578][0-9]{9})|((0\\d{2}-\\d{8})|(0\\d{3}-\\d{7,8})|(0\\d{10,11}))|(((400)-(\\d{3})-(\\d{4}))|((400)-(\\d{4})-(\\d{3}))|((400)-(\\d{5})-(\\d{2}))|(400\\d{7}))|(((800)-(\\d{3})-(\\d{4}))|((800)-(\\d{4})-(\\d{3}))|((800)-(\\d{5})-(\\d{2}))|(800\\d{7})))$"
letrefextePhone =NSPredicate(format:"SELF MATCHES %@", phone)
ifrefextePhone.evaluate(with:self) ==true{
returntrue
}
return false
}
2.移动电话
funcisMobilePhone()->Bool{
let mobilePhone = "^((1[34578][0-9]{9})|((0\\d{2}-\\d{8})|(0\\d{3}-\\d{7,8})|(0\\d{10,11}))$"
letrefexMobilePhone =NSPredicate(format:"SELF MATCHES %@", mobilePhone)
ifrefexMobilePhone.evaluate(with:self) ==true{returntrue}
return false
}
3. 车牌号验证
funcisValidCarNo() ->Bool{
let carRegex = "^[A-Za-z]{1}[A-Za-z_0-9]{5}$"
letcarTest =NSPredicate(format:"SELF MATCHES %@", carRegex)
returncarTest.evaluate(with:self)
}
4. 邮政编码
funcisValidPostalcode() ->Bool{
letpostalCodeRegex ="^[0-8]\\d{5}(?!\\d)$"
letpostalCodeTest =NSPredicate(format:"SELF MATCHES %@", postalCodeRegex)
returnpostalCodeTest.evaluate(with:self)
}
5.价格
funcisPrice() ->Bool{
letpostalCodeRegex ="^\\d+(\\.\\d+)?$"
letpostalCodeTest =NSPredicate(format:"SELF MATCHES %@", postalCodeRegex)
returnpostalCodeTest.evaluate(with:self)
}
6.数字和字母
varisBarCode:Bool{
letemailRegex ="^[A-Za-z0-9]+$"
return NSPredicate(format: "SELF MATCHES %@", emailRegex).evaluate(with: self)
}
7.数字
varisNumber:Bool{
letemailRegex ="^[0-9]*$"
return NSPredicate(format: "SELF MATCHES %@", emailRegex).evaluate(with: self)
}
8.Email validation
varisEmail:Bool{
let emailRegex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}"
return NSPredicate(format: "SELF MATCHES %@", emailRegex).evaluate(with: self)
}
9.验证密码是否符合规则 8-20位字符,必须包含字母和数字,字母区分大小写
varisPwdConformRule:Bool
{
let match = "(^(?=.*[0-9])(?=.*[a-zA-Z])(.{8,20})$)"
return NSPredicate(format: "SELF MATCHES %@", match).evaluate(with: self)
}
10. Email validation
varisPhoneNumber:Bool{
let phoneRegex = "^((1[34578][0-9]{9})|((0\\d{2}-\\d{8})|(0\\d{3}-\\d{7,8})|(0\\d{10,11}))|(((400)-(\\d{3})-(\\d{4}))|((400)-(\\d{4})-(\\d{3}))|((400)-(\\d{5})-(\\d{2}))|(400\\d{7}))|(((800)-(\\d{3})-(\\d{4}))|((800)-(\\d{4})-(\\d{3}))|((800)-(\\d{5})-(\\d{2}))|(800\\d{7})))$"
return NSPredicate(format: "SELF MATCHES %@", phoneRegex).evaluate(with: self)
}
11.是否是汉字
varisCarlisence:Bool{
let carlisence = "^([\\u4e00-\\u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{5})$"
return NSPredicate(format: "SELF MATCHES %@", carlisence).evaluate(with: self)
}
12.中文判断
varisNickName:Bool{
let nickNameReg = "^([\\u4E00-\\u9FA5]{2,30}$)"
return NSPredicate(format: "SELF MATCHES %@",nickNameReg).evaluate(with: self)
}
13.名字(汉字字母数字)
varisName:Bool{
let nameReg = "^([\\u4E00-\\u9FA5A-Za-z0-9_]{2,10}$)"
return NSPredicate(format: "SELF MATCHES %@",nameReg).evaluate(with: self)
}
14.是否存在特殊字符
varisHaveSpecialCharacter:Bool{
let nameReg = "^([\\u4E00-\\u9FA5A-Za-z0-9]*$)"
return NSPredicate(format: "SELF MATCHES %@",nameReg).evaluate(with: self)
}
15.验证是否有非字母和数字之外的字符
varisTaxNumber:Bool{
lettaxNumber ="^[A-Za-z0-9]+$"
return NSPredicate(format: "SELF MATCHES %@", taxNumber).evaluate(with: self)
}
16.验证是否有非字母数字下划线@之外的字符
varisEmailCharacter:Bool{
let taxNumber = "^[a-zA-Z0-9_@.]*$"
return NSPredicate(format: "SELF MATCHES %@", taxNumber).evaluate(with: self)
}
17.验证是否有非字母数字*_&@.,?!符号-之外的字符
varisTKHCharacter:Bool{
let taxNumber = "^[a-zA-Z0-9*_&@.,?!.]*$"
return NSPredicate(format: "SELF MATCHES %@", taxNumber).evaluate(with: self)
}
18.验证是否有非字母数字下划线之外的字符
varisUserNameCharacter:Bool{
lettaxNumber ="^[a-zA-Z0-9_]*$"
return NSPredicate(format: "SELF MATCHES %@", taxNumber).evaluate(with: self)
}
19.价格
funcisPrice() ->Bool{
let price = "^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))|(\\.[0-9]+)|[0-9]*[1-9][0-9]*\\.$"
return NSPredicate(format: "SELF MATCHES %@", price).evaluate(with: self)
}
20.身份证号
funcisIDCardNum() ->Bool{
let idCard = "(^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{2}[0-9Xx]$)"
return NSPredicate(format: "SELF MATCHES %@", idCard).evaluate(with: self)
}