iOS 正则表达式

1.导入工程

2.导入辅助库

libicucore.tbd

3.编译为非ARC        (-fno-objc-arc)

4.定义正则表达式

//用来匹配中文
        NSString * pattern=@"\[[\u4E00-\u9FA5]+\]";
 //待匹配字符串
        NSString * str=@"[哈哈]hello[你好]world[吗]good";

5.引入头文件调用如下方法

#import "RegexKitLite.h"
  //用来获取分割后的字符串
  [str enumerateStringsSeparatedByRegex:pattern usingBlock:^(NSInteger captureCount, NSString *const __unsafe_unretained *capturedStrings, const NSRange *capturedRanges, volatile BOOL *const stop) {
            NSLog(@"%@",*capturedStrings);
        }];
//用来获取匹配的字符串 [str enumerateStringsMatchedByRegex:pattern usingBlock:^(NSInteger captureCount, NSString *const __unsafe_unretained *capturedStrings, const NSRange *capturedRanges, volatile BOOL *const stop) { NSLog(@"%@",*capturedStrings); }];

 

//使用正则表达式做验证

    NSString *email = @"iMilo@163.com";

    NSString *regex = @"\b([a-zA-Z0-9%_.+\-]+)@([a-zA-Z0-9.\-]+?\.[a-zA-Z]{2,6})\b";   

   if ([email isMatchedByRegex:regex]){       

    NSLog(@"通过校验!");   

  }else{       

    NSLog(@"未通过校验,数据格式有误,请检查!");  

}

6.正则表达式详细网址: http://www.cnblogs.com/light169/archive/2006/10/18/532347.html

原文地址:https://www.cnblogs.com/MrFeng/p/4980665.html