我有一个存储在字符串中的html代码。现在我想从源代码中提取一个图像。
我之前使用的是REgExKitLite,但根据此链接http://www.cocoabuilder.com/archive/cocoa/288966-applications-using-regexkitlite-no-longer-being-accepted-at-the-appstore.html,如果我们想将我的应用提交到应用商店,建议不要使用REgExKitLite。
我只需要一个非常简单的实现,使用regEx从另一个字符串中提取一个字符串。大多数其他SO解决方案都试图完成相当复杂的任务,因此对于像我这样的初学者来说很难理解。
即使是一个很好的教程链接(关于NSRegularExpression实现)也可以。只要教程简单明了,我真的不介意阅读它并学习基础知识。日Thnx!
答案 0 :(得分:4)
在iOS 4.0+中,您可以使用 NSRegularExpression :
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"stack(.*).html" options:0 error:NULL];
NSString *str = @"stackoverflow.html";
NSTextCheckingResult *match = [regex firstMatchInString:str options:0 range:NSMakeRange(0, [str length])];
// [match rangeAtIndex:1] gives the range of the group in parentheses
// [str substringWithRange:[match rangeAtIndex:1]] gives the first captured group in this example
您可以按照此reference获取更多解决方案。