关闭

iOS 正则表达式的使用(Swift版)

68人阅读 评论(0) 收藏 举报
分类:

把以前oc时候写的一些正则用swift 写了下来。


///字符串
var sStr :String = "^\\w{2}$"
let sText :NSPredicate = NSPredicate.init(format: "SELF MATCHES %@", sStr)
let sValue = sText.evaluateWithObject("ab")
///邮箱
var emailStr :String = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
let emailText :NSPredicate = NSPredicate.init(format: "SELF MATCHES %@", emailStr)
let value = emailText.evaluateWithObject("gsn.1125@163.com")
///固定电话
var telStr :String = "^(0[0-9]{2,3}\\-)?([0-9][0-9]{6,7})+(\\-[0-9]{1,4})?$"
let telText :NSPredicate = NSPredicate.init(format: "SELF MATCHES %@", telStr)
let telValue = telText.evaluateWithObject("010-1234678")
///手机号
var phoneStr :String = "^1[3,5,6,7,8][0-9]\\d{8}"
let phoneText :NSPredicate = NSPredicate.init(format: "SELF MATCHES %@", phoneStr)
let phoneValue = phoneText.evaluateWithObject("18501003551")
///身份证号
var idCard:String = "^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$|^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|(?i)X)$"
let idCardText : NSPredicate = NSPredicate.init(format: "SELF MATCHES %@", idCard)
let idCardValue = idCardText.evaluateWithObject("20120120160829000x")




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:9081次
    • 积分:204
    • 等级:
    • 排名:千里之外
    • 原创:11篇
    • 转载:4篇
    • 译文:0篇
    • 评论:9条
    文章分类
    最新评论