打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
IOS正则表达式
  1. 在 iOS 开发中要用正则表达式的话,至少有三种选择:  
  2.   
  3. 1. 系统自带的  NSString 和 NSMutableString 相关的方法  
  4. 2. RegexKit Framework - http://regexkit.sourceforge.net  
  5. 3. RegexKitLite - http://regexkit.sourceforge.net/RegexKitLite/  
  6.   
  7. NSString 和 NSMutableString 里像  
  8.   
  9. 1  
  10. 2  
  11. 3  
  12. - (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask;  
  13. - (NSUInteger)replaceOccurrencesOfString:(NSString *)target withString:(NSString *)replacement  
  14.                                  options:(NSStringCompareOptions)options range:(NSRange)searchRange;  
  15. 带有 NSStringCompareOptions 参数的方法就可以支持正则表达式,只要对于 NSStringCompareOptions 参数取值中含有 NSRegularExpressionSearch 这个 Mask 值即可。  
  16.   
  17. RegexKit Framework 用的是 PCRE(Perl Compatible Regular Expressions) 库,可能是我们熟悉正则表达式语法,但需要引入 PCRE 静态库。  
  18.   
  19. RegexKitLite,看链接它与 RegexKit Framework 出自一家,但它用的是 Mac 自带的 ICU(International Components for Unicode) 库,所以语法上与 RegexKit Framework 是不一样的,且引入它也较简单。  
  20.   
  21. 只要下载到文件 RegexKitLite-4.0.tar.bz2,解压出其中的 RegexKitLite.h 和 RegexKitLite.m 两文件加到项目中来,再就记得加上系统自带的 libicucore.dylib 库就行。RegexKitLite 所有的方法也是对 NSString 和 NSMutableString 两个类的扩展方法,典型的方法:  
  22.   
  23. 1  
  24. 2  
  25. 3  
  26. 4  
  27. 5  
  28. - (BOOL)RKL_METHOD_PREPEND(isMatchedByRegex):(NSString *)regex;  
  29. - (NSRange)RKL_METHOD_PREPEND(rangeOfRegex):(NSString *)regex inRange:(NSRange)range;  
  30. - (NSInteger)RKL_METHOD_PREPEND(replaceOccurrencesOfRegex):(NSString *)regex withString:(NSString *)  
  31. - (NSArray *)RKL_METHOD_PREPEND(captureComponentsMatchedByRegex):(NSString *)regex;  
  32. ............................................  
  33. 真要说 RegexKitLite 的用法,也就又回到了正则表达式的写法,没什么可说的,只要参考:http://regexkit.sourceforge.net/RegexKitLite/,里面也有所有的扩展方法列表。  
  34.   
  35. 因此,这里只说一个如何进行不区分大小写的匹配,沿袭以往的经验,要么向 JS 那样 /abc/i,要么就是加上不区分大小写的标识参数,如:  
  36.   
  37. 1  
  38. 2  
  39. - (BOOL)RKL_METHOD_PREPEND(isMatchedByRegex):(NSString *)regex options:(RKLRegexOptions)options  
  40.                                      inRange:(NSRange)range error:(NSError **)error;  
  41. 中的 (RKLRegexOptions)options 参数,可以支持:  
  42.   
  43. RKLNoOptions, RKLCaseless, RKLComments, RKLDotAll, RKLMultiline, RKLUnicodeWordBoundarie  
  44.   
  45. 这个组合,只要标识上 RKLCaseless。然而你也许发现了如果不区分大小写的匹配,调用方法只是:  
  46.   
  47. 1  
  48. - (BOOL)RKL_METHOD_PREPEND(isMatchedByRegex):(NSString *)regex;  
  49. 就因为要支持大小写不敏感而必须用另一个参数多几个的重载方法,要是想要应用 replaceOccurrencesOfRegex 方法,大小写不敏感比不敏感的方法版本参数多的更多,实际并不用关心那些多余的参数,甚是不便。  
  50.   
  51. 本文原始链接:http://unmi.cc/ios-regexkitlite-library, 来自:隔叶黄莺 Unmi Blog  
  52. 那么能不能像 JS 的正则表达式那样把约束标识写在正则表达式里呢,可以的,请试一下:  
  53.   
  54. 1  
  55. 2  
  56. 3  
  57. 4  
  58. 5  
  59. 6  
  60. 7  
  61. 8  
  62. if([@"Unmi" isMatchedByRegex:@"(?i)uNmI"]){  
  63.     NSLog(@"Matched");  
  64. }  
  65.    
  66. //下面的代码一样的效果  
  67. if([@"Unmi" isMatchedByRegex:@"(?i:uNmI)"]){  
  68.     NSLog(@"Matched");  
  69. }  
  70. 会输出 Matched,匹配成功,关键就是表达式中的 (?i)Xxx 或者 (?i:Xxx) 表明这个表达式不区分大小写,可以查看下这个帮助原文:  
  71.   
  72. Options for controlling the behavior of a regular expression pattern can be controlled in two ways. When the method supports it, options may specified by combining RKLRegexOptions flags with the C bitwise OR operator. For example:  
  73.   
  74. matchedRange = [aString rangeOfRegex:@"^(blue|red)$" options:(RKLCaseless | RKLMultiline) inRange:range error:NULL];  
  75. The other way is to specify the options within the regular expression itself, of which there are two ways. The first specifies the options for everything following it, and the other sets the options on a per capture group basis. Options are either enabled, or following a -, disabled. The syntax for both is nearly identical:  
  76.   
  77. Option  Example Description  
  78. (?ixsmw-ixsmw)… (?i)…   Enables the RKLCaseless option for everything that follows it. Useful at the beginning of a regular expression to set the desired options.  
  79. (?ixsmw-ixsmw:…)    (?iw-m:…)   Enables the RKLCaseless and RKLUnicodeWordBoundaries options and disables RKLMultiline for the capture group enclosed by the parenthesis.  
  80. The following table lists the regular expression pattern option character and its corresponding RKLRegexOptions flag:  
  81.   
  82. Character   Option  
  83. i   RKLCaseless  
  84. x   RKLComments  
  85. s   RKLDotAll  
  86. m   RKLMultiline  
  87. w   RKLUnicodeWordBoundaries  
  88. 上面就是说对正则表达式的控件可以有两种选择,用 RKLRegexOptions 标识,或在表达式中用 (?..) 的写法。第二种选择又有两种写法,一种是 (?i)...  和 (?i:...)。问号后支持 i、x、s、m 和 w 五个标识,各自的意义看上面的表格,在标识前加个减号 - 表示去除该特性。  
  89.   
  90. 附上一些常用的 ICU 格式正则表达式:  
  91.   
  92. Network and URL  
  93. Description Regex  
  94. HTTP    \bhttps?://[a-zA-Z0-9\-.]+(?:(?:/[a-zA-Z0-9\-._?,'+\&%$=~*!():@\\]*)+)?  
  95. HTTP    \b(https?)://([a-zA-Z0-9\-.]+)((?:/[a-zA-Z0-9\-._?,'+\&%$=~*!():@\\]*)+)?  
  96. HTTP    \b(https?)://(?:(\S+?)(?::(\S+?))?@)?([a-zA-Z0-9\-.]+)(?::(\d+))?((?:/[a-zA-Z0-9\-._?,'+\&%$=~*!():@\\]*)+)?  
  97. E-Mail  \b([a-zA-Z0-9%_.+\-]+)@([a-zA-Z0-9.\-]+?\.[a-zA-Z]{2,6})\b  
  98. Hostname    \b(?:[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}?[a-zA-Z0-9]\.)+[a-zA-Z]{2,6}\b  
  99. IP  \b(?:\d{1,3}\.){3}\d{1,3}\b  
  100. IP with Optional Netmask    \b((?:\d{1,3}\.){3}\d{1,3})(?:/(\d{1,2}))?\b  
  101. IP or Hostname  \b(?:(?:\d{1,3}\.){3}\d{1,3}|(?:[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}?[a-zA-Z0-9]\.)+[a-zA-Z]{2,6})\b  
  102. 上面的正则表达式实际书写时需把一个反斜杠替换成两个反斜杠,如匹配 E-Mail 时要写成  
  103.   
  104. 1  
  105. 2  
  106. 3  
  107. 4  
  108. 5  
  109. NSString *regex = @"\\b([a-zA-Z0-9%_.+\\-]+)@([a-zA-Z0-9.\\-]+?\\.[a-zA-Z]{2,6})\\b";  
  110. NSLog(@"Regex: %@", regex);  
  111. if([@"unmi@scalau.com" isMatchedByRegex:regex]){  
  112.     NSLog(@"Valid email address");  
  113. }  
  114. 上面的代码输出:  
  115.   
  116. Regex: \b([a-zA-Z0-9%_.+\-]+)@([a-zA-Z0-9.\-]+?\.[a-zA-Z]{2,6})\b  
  117. Valid email address  
  118.   
  119. 这和 Java 的正则表达式一样,也就是要写成 NSLog 输出的样子,也就是转义的转义,如果是 \- 或 \. 编译时会提示:  
  120.   
  121. Lexical or Preprocessor Issue Unknown escape sequence "\."  
  122. Lexical or Preprocessor Issue Unknown escape sequence "\-"  
  123.   
  124. 但执行时不会报错,只是匹配不成功.  
  125.   
  126. 记住,RegexKitLite 的完全帮助手册尽在:http://regexkit.sourceforge.net/RegexKitLite/  
  127.   
  128. 本文链接 http://unmi.cc/ios-regexkitlite-library, 来自 隔叶黄莺 Unmi Blog  

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
iOS中正则表达式的基本使用方法
正则表达式来了,Excel中的正则表达式匹配示例
看完还不会「正则表达式」你打我
正则表达式 学习资料整理
正则表达之
理解 Java 正则表达式怪异的 \ 和 \\,让您见怪不怪 | 隔叶黄莺 Unmi Bl...
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服