IOS开发之常用的正则表达式

IOS开发之常用的正则表达式,第1张

概述[objc]  view plain copy iOS 中可以通过 NSPredicate 来处理正则表达式。相关资料如下: NSPredicate 苹果官方文档: http://developer.apple.com/documentation/Cocoa/Conceptual/Predicates/predicates.html Predicate format strings: http:/ [objc]  view plain copy

iOS 中可以通过 nspredicate 来处理正则表达式。相关资料如下:

nspredicate 苹果官方文档:
http://developer.apple.com/documentation/Cocoa/Conceptual/Predicates/predicates.html

Predicate format strings:
http://developer.apple.com/documentation/Cocoa/Conceptual/Predicates/Articles/pSyntax.html

ICU 正则表达式规则:
http://www.icu-project.org/userguide/regexp.html


在 iOS 中,我们使用 nspredicate 的字符串比较功能来进行正则表达式处理,其比较关键字为:MATCHES

下面,列举一个匹配6-15个由字母/数字组成的字符串的正则表达式,来看看 nspredicate 的具体使用:

[cpp]  view plain copy print ? Nsstring * regex        = @"(^[A-Za-z0-9]{6,15}$)";   nspredicate * pred      = [nspredicate predicateWithFormat:@"SELF MATCHES %@", regex];   BOol isMatch            = [pred evaluateWithObject:@"123456ABCde"];  
下面是一些常用的正则表达式 //邮箱   + (BOol) valIDateEmail:(Nsstring *)email   {       Nsstring *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}";       nspredicate *emailTest = [nspredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];       return [emailTest evaluateWithObject:email];   }         //手机号码验证   + (BOol) valIDateMobile:(Nsstring *)mobile   {       //手机号以13, 15,18开头,八个 \d 数字字符       Nsstring *phoneRegex = @"^((13[0-9])|(15[^4,\D])|(18[0,0-9]))\d{8}$";       nspredicate *phoneTest = [nspredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];       return [phoneTest evaluateWithObject:mobile];   //车牌号验证   + (BOol) valIDateCarNo:(Nsstring *)carNo       Nsstring *carRegex = @"^[\u4e00-\u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{4}[a-zA-Z_0-9_\u4e00-\u9fa5]$";       nspredicate *carTest = [nspredicate predicateWithFormat:@"SELF MATCHES %@",carRegex];       NSLog(@"carTest is %@",carTest);       return [carTest evaluateWithObject:carNo];   //车型   + (BOol) valIDateCarType:(Nsstring *)CarType       Nsstring *CarTypeRegex = @"^[\u4E00-\u9FFF]+$";       return [carTest evaluateWithObject:CarType];   }   //用户名   + (BOol) valIDateUsername:(Nsstring *)name       Nsstring *usernameRegex = @"^[A-Za-z0-9]{6,20}+$";       nspredicate *usernamePredicate = [nspredicate predicateWithFormat:@"SELF MATCHES %@",usernameRegex];       BOol B = [usernamePredicate evaluateWithObject:name];       return B;   //密码   + (BOol) valIDatePassword:(Nsstring *)passWord       Nsstring *passWordRegex = @"^[a-zA-Z0-9]{6,108); List-style:decimal-leading-zero outsIDe; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">     nspredicate *passWordPredicate = [nspredicate predicateWithFormat:@"SELF MATCHES %@",passWordRegex];       return [passWordPredicate evaluateWithObject:passWord];   //昵称   + (BOol) valIDateNickname:(Nsstring *)nickname       Nsstring *nicknameRegex = @"^[\u4e00-\u9fa5]{4,8}$";       nspredicate *passWordPredicate = [nspredicate predicateWithFormat:@"SELF MATCHES %@",nicknameRegex];       return [passWordPredicate evaluateWithObject:nickname];   //身份z号   + (BOol) valIDateIDentityCard: (Nsstring *)IDentityCard       BOol flag;       if (IDentityCard.length <= 0) {           flag = NO;           return flag;       }       Nsstring *regex2 = @"^(\d{14}|\d{17})(\d|[xX])$";       nspredicate *IDentityCardPredicate = [nspredicate predicateWithFormat:@"SELF MATCHES %@",regex2];       return [IDentityCardPredicate evaluateWithObject:IDentityCard];   }  

其实iOS中有三种方式来实现正则表达式的匹配。现在将他们都记录在这里:

1.利用nspredicate(谓词)匹配

例如匹配有效邮箱:

Nsstring *email = @“nijino_saki@163.com”;

    Nsstring *regex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";

    nspredicate *predicate = [nspredicate predicateWithFormat:@"SELF MATCHES %@",regex];

    BOol isValID = [predicate evaluateWithObject:email];

谓词匹配比较灵活,但是需要有谓词的相关知识。


2.利用rangeOfString:option:直接查找

    Nsstring *searchText = @"// Do any additional setup after loading the vIEw,typically from a nib.";

    NSRange range = [searchText rangeOfString:@"(?:[^,])*\\." options:NSRegularExpressionSearch];

    if (range.location != NSNotFound) {

        NSLog(@"%@",[searchText substringWithRange:range]);

    }

options中设定NSRegularExpressionSearch就是表示利用正则表达式匹配,会返回第一个匹配结果的位置。


3.使用正则表达式类

    NSError *error = NulL;

    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?:[^,])*\\." options:NSRegularExpressionCaseInsensitive error:&error];

    NSTextCheckingResult *result = [regex firstMatchInString:searchText options:0 range:NSMakeRange(0,[searchText length])];

    if (result) {

        NSLog(@"%@\n",[searchText substringWithRange:result.range]);

使用系统的正则表达式类(NSRegularExpression)会返回匹配的多个结果。

小结:

第一种匹配需要学习nspredicate的写法,需要查阅苹果相关技术文档;如果只关心第一个匹配的结果,第二种匹配较为简洁;如果需要匹配多个结果,同时匹配多次,第三种方式效率会更高。

总结

以上是内存溢出为你收集整理的IOS开发之常用的正则表达式全部内容,希望文章能够帮你解决IOS开发之常用的正则表达式所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1052351.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存