1

将HTML解析为字符串时存在一些问题。我有一个很大的字符串,我需要删除“脚本”标记之间的字符。就像这样:NSRegularExpression(iOS)

<script...>Some text here</script> 

所以我需要删除“这里的一些文字”。我认为使用NSRegularExpression会很好。有谁能够帮助我 ?非常感谢。

回答

0

虽然你一般会建议不解析使用正则表达式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]]); 
        }]; 
+0

感谢,它的工作好! – Seliver