D
D
deleted-mezhevikin2013-10-30 14:42:01
iOS
deleted-mezhevikin, 2013-10-30 14:42:01

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>";

so
NSString *pattern = @"<script type=\"text/json\" id=\"disqus-threadData\">(.*?)</script>";

HTML example: rghost.ru/49821723

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
byss, 2013-10-30
@deleted-mezhevikin

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;
}

It looks like it works.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question