Answer the question
In order to leave comments, you need to log in
How to properly pattern NSRegularExpression?
From the c html line, you need to get the contents of the script tag with id="disqus-threadData".
Inside the tag is json.
Tried like this:
NSString *pattern = @"<script type=\"text/json\" id=\"disqus-threadData\">([^\"]+)</script>";
NSString *pattern = @"<script type=\"text/json\" id=\"disqus-threadData\">(.*?)</script>";
Answer the question
In order to leave comments, you need to log in
int main () {
NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:@"/Users/byss/Downloads/test.html"];
NSString *haystack = [[NSString alloc] initWithData:[handle readDataToEndOfFile] encoding:NSUTF8StringEncoding];
NSRegularExpression *re = [[NSRegularExpression alloc] initWithPattern:@"<script type=\"text/json\" id=\"disqus-threadData\">(.+?)</script>" options:NSRegularExpressionCaseInsensitive error:nil];
[re enumerateMatchesInString:haystack options:0 range:NSMakeRange (0, haystack.length) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
if (result.numberOfRanges == 2) {
NSLog (@"%@", [haystack substringWithRange:[result rangeAtIndex:1]]);
}
}];
return 0;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question