您的位置:首页 > 移动开发 > Objective-C

Objective-C 正则表达式的使用

2015-06-16 15:28 781 查看
前言:

编程中,对于字符串的处理是无处不在的.时常需要在一堆乱码中找到有用的信息.比如在如下的字符串中获取有效的 URL
Email


ababsdbasbdabdhttp://baidu.com
<div>test@test.com<div>

诸如此类问题,我们都可以通过正则表达式来解决,正则在任意编程语言都有对应实现.

iOS4 之后,Cocoa 也提供了用于正则的
NSRegularExpression

NSTextCheckingResult
这两个类.前者用于创建正则,匹配,替换;后者是一个快速匹配对象,可用于检查拼写,URL等,也可作为正则匹配的结果,包含匹配到子串所在 range 和匹配规则.

创建:

NSRegularExpression * regex = [NSRegularExpression regularExpressionWithPattern:@"http[s]?://[A-z0-9.-]*"
options:NSRegularExpressionCaseInsensitive error:nil];

正则匹配模式

[] 表示集合,
* 表示 出现零次或多次,
? 表示出现零次或一次,
http[s]?://[A-z0-9.-]* ,即匹配以 http://https:// 开头,含有零或多个字母 - . 的字符串

option

NSRegularExpressionCaseInsensitive //忽略大小写
NSRegularExpressionAllowCommentsAndWhitespace //忽略空格,回车,tab,注释
NSRegularExpressionIgnoreMetacharacters //忽略 Meta
NSRegularExpressionDotMatchesLineSeparators //允许使用 . 通配任何符号,包括换行符
NSRegularExpressionAnchorsMatchLines //允许使用 ^ $匹配行头和行尾
NSRegularExpressionUseUnixLineSeparators //只把 \n 当做换行符
NSRegularExpressionUseUnicodeWordBoundaries

应用:

NSRegularExpression
提供了对字符串
匹配 , 替换 的函数,

NSRegularExpression
(NSMatching)


//枚举在 string 的指定 range 中按 pattern 匹配到的所有子字符串,在 block 中做处理
- (void)enumerateMatchesInString:(NSString *)string options:(NSMatchingOptions)options range:(NSRange)range usingBlock:(void (^)(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop))block;
//获取在 string 的指定 range 中按 pattern 匹配到的所有子字符串
- (NSArray *)matchesInString:(NSString *)string options:(NSMatchingOptions)options range:(NSRange)range;
//获取匹配到子串的个数
- (NSUInteger)numberOfMatchesInString:(NSString *)string options:(NSMatchingOptions)options range:(NSRange)range;
//获取第一次匹配到的结果,
- (NSTextCheckingResult *)firstMatchInString:(NSString *)string options:(NSMatchingOptions)options range:(NSRange)range;
//获取第一次匹配到的 range
- (NSRange)rangeOfFirstMatchInString:(NSString *)string options:(NSMatchingOptions)options range:(NSRange)range;

NSRegularExpression
(NSReplacement)


//修改在 string 的指定 range 中按 pattern 匹配到的子字符串为 templ, 将修改后的字符串深 copy 后返回
- (NSString *)stringByReplacingMatchesInString:(NSString *)string options:(NSMatchingOptions)options range:(NSRange)range withTemplate:(NSString *)templ;
//修改在 可变 string 的指定 range 中按 pattern 匹配到的子字符串为 templ, 并返回所修改的子串的个数
- (NSUInteger)replaceMatchesInString:(NSMutableString *)string options:(NSMatchingOptions)options range:(NSRange)range withTemplate:(NSString *)templ;

匹配示例

NSMutableString * str = [@"ababsdbasbdabdhttp://baidu.com" mutableCopy];
NSRegularExpression * regex = [NSRegularExpression regularExpressionWithPattern:@"http[s]?://[A-z0-9.]*" options:NSRegularExpressionCaseInsensitive error:nil];
NSTextCheckingResult * r =[regex firstMatchInString:str options:NSMatchingReportCompletion range:NSMakeRange(0, str.length)];
NSLog(@"%@",[str substringWithRange:r.range]);
输出:
-05-29 11:27:05.426 RegexDemo[49294:303] http://baid-u1.com[/code] 

参考:

http://userguide.icu-project.org/strings/regexphttps://developer.apple.com/library/mac/documentation/Foundation/Reference/NSRegularExpression_Class/Reference/Reference.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: