将HTML解析为string有一个小问题。我有一个大字符串,我需要删除字符之间的“脚本”标签。就像这样:
<script...>Some text here</script>所以我要删除“这里的一些文字”。我认为使用NSRegularExpression是很棒的。有人能帮我吗?非常感谢。
发布于 2014-03-06 12:23:15
虽然通常建议您不要使用正则表达式解析HTML (请参阅my all-time favorite S.O. answer),但您可以使用如下方法来近似它:
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<script.*?>(.*?)</script>"
options:NSRegularExpressionCaseInsensitive | NSRegularExpressionDotMatchesLineSeparators
error:&error];
[regex enumerateMatchesInString:string
options:0
range:NSMakeRange(0, [string length])
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
NSLog(@"%@", [string substringWithRange:[result rangeAtIndex:1]]);
}];https://stackoverflow.com/questions/22223780
复制相似问题