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