Using Regular Expression

很好的入门教程:《正则表达式30分钟入门教程》 by deerchao

维基百科:http://en.wikipedia.org/wiki/Regular_expression

正则表达式在线测试:http://tool.chinaz.com/regex/

三种使用方法

1. NSPredicate

一般匹配,不正规地匹配Email(正规方法请查看最后的Using a regular expression to validate an email address

//get email

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",@"^[a-zA-Z0-9_\\.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-\\.]+$"];
BOOL result = [predicate evaluateWithObject:email];

作为Core Data条件语句:

//context setting

//get topic object

NSError *error;

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Favorite"];

request.predicate = [NSPredicate predicateWithFormat:@"topicId = %@",topic.topicId];

NSArray *resultArr = [self.context executeFetchRequest:request error:&error];

2. - rangeOfString: options: range:

NSString regexPattern = @"(?<=href[\\s='\"]{1,6})[^='\">\\ ]+[^'\">\\ ]*";
NSRange hrefRange = [HTMLString rangeOfString:regexPattern options:(NSRegularExpressionSearch|NSCaseInsensitiveSearch) range:match.range];

if (!NSEqualRanges(hrefRange, NSMakeRange(NSNotFound, 0))){
    // found!
}

3. NSRegularExpression

NSString *contentSeparatePattern = @"(?<=\\<tr class=\"(reply|comment)_)\\d+";
NSRegularExpression *contentSeparateRegex = [NSRegularExpression regularExpressionWithPattern:contentSeparatePattern options:NSRegularExpressionCaseInsensitive error:&regexError];

//匹配整个字符串并输出所有结果,返回NSArray<NSTextCheckingResult *> *
NSArray *matches = [contentSeparateRegex matchesInString:HTMLString options:0 range:NSMakeRange(0, HTMLString.length)];

//取第一个匹配值,返回NSTextCheckingResult
NSTextCheckingResult *contentSeparateTextCheckingResult = [contentSeparateRegex firstMatchInString:HTMLString options:0 range:NSMakeRange(0, HTMLString.length)];

//取第一个匹配值的具体范围,返回NSRange
NSRange contentSeparateRange = [contentSeparateRegex  rangeOfFirstMatchInString:HTMLString options:0 range:NSMakeRange(0, HTMLString.length)];

3.1. 使用匹配分组,一次匹配出多个结果

关于匹配分组看这里:正则表达式的分组

使用 NSTextCheckingResult- rangeAtIndex: 方法:

A result must have at least one range, but may optionally have more (for example, to represent regular expression capture groups).

Passing rangeAtIndex: the value 0 always returns the value of the the range property. Additional ranges, if any, will have indexes from 1 to numberOfRanges-1.

NSString *target = @"sfd-222-333-444-sadkgh";

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"-(\\d+)-(\\d+)-(\\d+)-" options:0 error:nil];

NSString *unitFloorNumber,*unitAuthorName,*unitReplyDate;

NSArray *resultArr = [regex matchesInString:target options:0 range:NSMakeRange(0, target.length)];
for (NSTextCheckingResult *result in resultArr) {
    if (NSEqualRanges(result.range,NSMakeRange(NSNotFound, 0))){
        continue;
    }
    if (result.numberOfRanges > 3){
        unitFloorNumber = [target substringWithRange:[result rangeAtIndex:1]];//222
        unitAuthorName = [target substringWithRange:[result rangeAtIndex:2]];//333
        unitReplyDate = [target substringWithRange:[result rangeAtIndex:3]];//444
    }
}

一些相关链接:

标签:object-c