iOS/iPhone:从正则表达式中提取组(NSRegularExpression)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS/iPhone:从正则表达式中提取组(NSRegularExpression)相关的知识,希望对你有一定的参考价值。
Extract groups from a Regular Expression
NSRegularExpression* exp = [NSRegularExpression regularExpressionWithPattern:@"(put)-(expression)-(here)" options:NSRegularExpressionSearch error:&error]; if (error) { NSLog(@"%@", error); } else { NSTextCheckingResult* result = [exp firstMatchInString:testString options:0 range:NSMakeRange(0, [productDescription length] ) ]; if (result) { NSRange groupOne = [result rangeAtIndex:1]; // 0 is the WHOLE string. NSRange groupTwo = [result rangeAtIndex:2]; NSRange groupThree = [result rangeAtIndex:3]; NSLog( [testString substringWithRange:groupOne] ); NSLog( [testString substringWithRange:groupTwo] ); NSLog( [testString substringWithRange:groupThree] ); } }
以上是关于iOS/iPhone:从正则表达式中提取组(NSRegularExpression)的主要内容,如果未能解决你的问题,请参考以下文章