Parsing HTML NSRegularExpression

i’m trying to parse an HTML page using NSRegularExpressions..
The page is a repetition of this html code:

<div class="fact" id="fact66">STRING THAT I WANT</div> <div class="vote">
<a href="index.php?p=detail_fact&fact=106">#106</a> &nbsp; &nbsp; 
<span id="p106">246080 / 8.59  </span> &nbsp; &nbsp;
<span id="f106" class="vote2">
<a href="#" onclick="xajax_voter(106,3); return false;">(+++)</a> 
<a href="#" onclick="xajax_voter(106,2); return false;">(++)</a>  
<a href="#" onclick="xajax_voter(106,1); return false;">(+)</a> 
<a href="#" onclick="xajax_berk(106); return false;">(-)</a></span>
<span id="ve106"></span>

So, i’ld like to get the string between the div

  • Cocoa error 2048 when using NSRegularExpression in Cocoa
  • Regular expression in ios to extract href url and discard rest of anchor tag?
  • How to write regular expressions in Objective C (NSRegularExpression)?
  • Regular expressions in an Objective-C Cocoa application
  • Swift replace substring regex
  • Regular Expressions in Objective-C and Core Data
  •  <div class="fact" id="fact66">STRING THAT I WANT</div>

    So i made a regex that looks like this

    <div class="fact" id="fact[0-9].*\">(.*)</div>

    Now, in my code, i implement it using this:

        NSString *htmlString = [NSString stringWithContentsOfURL:[NSURL URLWithString:@""] encoding:NSASCIIStringEncoding error:nil];
    NSRegularExpression* myRegex = [[NSRegularExpression alloc] initWithPattern:@"<div class=\"fact\" id=\"fact[0-9].*\">(.*)</div>\n" options:0 error:nil];
        [myRegex enumerateMatchesInString:htmlString options:0 range:NSMakeRange(0, [htmlString length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop) {
            NSRange range = [match rangeAtIndex:1];
            NSString *string =[htmlString substringWithRange:range];

    But it returns nothing… I tested my regex in Java and PHP and it works great, what am i doing wrong ?


    Solutions Collect From Internet About “Parsing HTML NSRegularExpression”

    Try using this regex:

     @"<div class=\"fact\" id=\"fact[0-9]*\">([^<]*)</div>"



    means: fact followed by a number between 0 and 9, followed by any character repeated any number of times.

    I also suggest using:


    instead of


    to match between the two divs so to deal with regex greediness, or alternatively:


    (? will make the regex non-greedy, so it stops at the first instance of </div>.