iOS开发中使用正则表达式

一.NSString自带的正则查找,替换方法

正则查找方法
– rangeOfString:options:
– rangeOfString:options:range:
– rangeOfString:options:range:locale:

正则替换方法
– stringByReplacingOccurrencesOfString:withString:options:range:

options参数指定搜索选项,类型为NSStringCompareOptions,可通过位或操作指定为NSCaseInsensitiveSearchNSLiteralSearchNSBackwardsSearchNSAnchoredSearch>等选项的组合。
若指定的选项为NSRegularExpressionSearch,则搜索字符串被认为是ICU兼容的正则表达式,如果指定了此选项,则与其可以同时存在的选项只有NSCaseInsensitiveSearchNSAnchoredSearch

二.使用RegexKitLite

RegexKitLite向标准NSString类增加了很多方法来使用正则表达式,RegexKitLite使用iOS系统自带的ICU(International Components for Unicode)正则引擎处理正则表达式,所以RegexKitLite使用的正则语法为ICU的语法,使用RegexKitLite需要导入libicucore.dylib库。

使用RegexKitLite的方法很简单,将RegexKitLite.h和RegexKitLite.m加入到工程,然后引入libicucore.dylib库即可。
RegexKitLite.h RegexKitLite.m

RegexKitLit NSString方法参考
RegexKitLite NSString Additions Reference

RegexKitLite的使用说明见:
Using RegexKitLite

ICU正则语法为:
ICU Syntax
ICU User Guide – Regular Expressions

三.使用RegexKit.framework框架

RegexKit Framework与RegexKitLite来自同一体系,但其更复杂和强大。RegexKit Framework不使用iOS系统的ICU正则库,而是自带 PCRE(Perl Compatible Regular Expressions)库, 所以其正则语法是PCRE的语法。

RegexKit Framework功能很强大,其向NSArray,NSData,NSDictionary,NSSet和NSString对象增加了正则表达式的支持。

TRegexKit.framework与RegexKitLite的区别
 RegexKit.frameworkRegexKitLite
Regex LibraryPCREICU
Library IncludedYes, built into framework object file.No, provided by Mac OS X.
Library Linked AsStatically linked into framework.Dynamically linked to/usr/lib/libicucore.dylib.
Compiled SizeApproximately 371KB per architecture.Very small, approximately 16KB—20KB per architecture.
StyleExternal, linked to framework.Compiled directly in to final executable.
Feature SetLarge, with additions to many classes.Minimal, NSString only.


四.常用ICU正则匹配模式

常用的ICU正则匹配模式见:
RegexKitLite Cookbook

数字 Numbers
DescriptionRegexExamples
Integer[+\-]?[0-9]+123-42+23
Hex Number0[xX][0-9a-fA-F]+0×00xdeadbeef0xF3
Floating Point[+\-]?(?:[0-9]*\.[0-9]+|[0-9]+\.)123..123+.42
Floating Point with Exponent[+\-]?(?:[0-9]*\.[0-9]+|[0-9]+\.)(?:[eE][+\-]?[0-9]+)?123..12310.0E131.23e-7
Comma Separated Number[0-9]{1,3}(?:,[0-9]{3})*421,2341,234,567
Comma Separated Number[0-9]{1,3}(?:,[0-9]{3})*(?:\.[0-9]+)?421,2341,234,567.89


文本文件 Text Files
DescriptionRegex
Empty Line(?m:^$)
Empty or Whitespace Only Line(?m-s:^\s*$)
Strip Leading Whitespace(?m-s:^\s*(.*?)$)
Strip Trailing Whitespace(?m-s:^(.*?)\s*$)
Strip Leading and Trailing Whitespace(?m-s:^\s*(.*?)\s*$)
Quoted String, Can Span Multiple Lines, May Contain \""(?:[^"\\]*+|\\.)*"
Quoted String, Single Line Only, May Contain \""(?:[^"\\\r\n]*+|\\[^\r\n])*"
HTML Comment(?s:<--.*?-->)
Perl / Shell Comment(?m-s:#.*$)
C, C++, or ObjC Comment(?m-s://.*$)
C, C++, or ObjC Comment and Leading Whitespace(?m-s:\s*//.*$)
C, C++, or ObjC Comment(?s:/\*.*?\*/)


网络与URL相关 Network and URL
DescriptionRegex
HTTP\bhttps?://[a-zA-Z0-9\-.]+(?:(?:/[a-zA-Z0-9\-._?,'+\&%$=~*!():@\\]*)+)?
HTTP\b(https?)://([a-zA-Z0-9\-.]+)((?:/[a-zA-Z0-9\-._?,'+\&%$=~*!():@\\]*)+)?
HTTP\b(https?)://(?:(\S+?)(?::(\S+?))?@)?([a-zA-Z0-9\-.]+)(?::(\d+))?((?:/[a-zA-Z0-9\-._?,'+\&%$=~*!():@\\]*)+)?
E-Mail\b([a-zA-Z0-9%_.+\-]+)@([a-zA-Z0-9.\-]+?\.[a-zA-Z]{2,6})\b
Hostname\b(?:[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}?[a-zA-Z0-9]\.)+[a-zA-Z]{2,6}\b
IP\b(?:\d{1,3}\.){3}\d{1,3}\b
IP with Optional Netmask\b((?:\d{1,3}\.){3}\d{1,3})(?:/(\d{1,2}))?\b
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


五.贪婪匹配与最小匹配

在正则表达式中单独使用*或+时,默认是匹配尽可能多的数据,即贪婪匹配。

*	Match zero or more times. Match as many times as possible.
+	Match one or more times. Match as many times as possible.

比如对 abcdefgabcdefg 使用abc(.*)g进行匹配,则捕获到到的数据为 defgabcdef。
若只想捕获到第一个g,即只想得到def,则需要使用最小匹配,在*或+后面加上?,即使用abc(.*?)g进行匹配。

*?	Match zero or more times. Match as few times as possible.
+?	Match one or more times. Match as few times as possible.

另外,在正则中用(…)包含内容是要捕获的数据,如果只要用(…)来引用group而不想捕获则可使用(?:…)。

(…)	Capturing parentheses. Range of input that matched the parenthesized subexpression is available after the match.

(?:…)	Non-capturing parentheses. Groups the included pattern, but does not provide capturing of matching text. Somewhat more efficient than capturing parentheses.

六.正则表达式书写格式

在书写正则表达式时,需要将\进行转义,即写成两个\\。
例如 匹配IP地址的正则表达式为 \b(?:\d{1,3}\.){3}\d{1,3}\b,则在实际书写时则为

 NSString *regex = @"\\b(?:\\d{1,3}\.){3}\\d{1,3}\\b";

参考:
iOS 开发中使用正则表达式-暨 RegexKitLite 库的用法
RegexKitLite Documentation
[perl]理解贪婪匹配和最小匹配之间的区别
NSString Class Reference
ICU – International Components for Unicode

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值