当前位置: 移动技术网 > 移动技术>移动开发>IOS > iOS App开发中Objective-C使用正则表达式进行匹配的方法

iOS App开发中Objective-C使用正则表达式进行匹配的方法

2019年07月24日  | 移动技术网移动技术  | 我要评论

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.使用正则表达式类

nsstring *searchtext = @"// do any additional setup after loading the view, typically from a nib.";  
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)会返回匹配的多个结果。

实例:
1.验证邮箱

+ (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];
}

nspredicate是一个foundation类,是用来查询的,原理和用法都类似于sql中的where。

2.验证手机号

简单的判断方法

+ (bool)validatephone:(nsstring *)phone
{
  nsstring *phoneregex = @"1[3|5|7|8|][0-9]{9}";
  nspredicate *phonetest = [nspredicate predicatewithformat:@"self matches %@", phoneregex];
  return [phonetest evaluatewithobject:phone];
}

这只是简单地判断手机号格式。其实手机的格式还是有一点复杂的。

详细的判断方法

//正则判断手机号码格式
+ (bool)validatephone:(nsstring *)phone
{
    /**
    * 手机号码
    * 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
    * 联通:130,131,132,152,155,156,185,186
    * 电信:133,1349,153,180,189
    */
    nsstring * mobile = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
    /**
         * 中国移动:china mobile
         * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
         */
    nsstring * cm = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
    /**
         * 中国联通:china unicom
         * 130,131,132,152,155,156,185,186
         */
    nsstring * cu = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
    /**
         * 中国电信:china telecom
         * 133,1349,153,180,189
         */
    nsstring * ct = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";
    /**
         * 大陆地区固话及小灵通
         * 区号:010,020,021,022,023,024,025,027,028,029
         * 号码:七位或八位
         */
   // nsstring * phs = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";

   nspredicate *regextestmobile = [nspredicate predicatewithformat:@"self matches %@", mobile];
   nspredicate *regextestcm = [nspredicate predicatewithformat:@"self matches %@", cm];
   nspredicate *regextestcu = [nspredicate predicatewithformat:@"self matches %@", cu];
   nspredicate *regextestct = [nspredicate predicatewithformat:@"self matches %@", ct];

  if (([regextestmobile evaluatewithobject:phone] == yes)
  || ([regextestcm evaluatewithobject:phone] == yes)
  || ([regextestct evaluatewithobject:phone] == yes)
  || ([regextestcu evaluatewithobject:phone] == yes))
  {
    if([regextestcm evaluatewithobject:phone] == yes) {
     nslog(@"china mobile");
    } else if([regextestct evaluatewithobject:phone] == yes) {
     nslog(@"china telecom");
    } else if ([regextestcu evaluatewithobject:phone] == yes) {
     nslog(@"china unicom");
    } else {
     nslog(@"unknow");
    }

    return yes;
  }
  else 
  {
    return no;
  }
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网