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>
<span id="p106">246080 / 8.59 </span>
<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>
</div>
So, i'ld like to get the string between the div
<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:@"http://www.myurl.com"] 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];
NSLog(string);
}];
But it returns nothing... I tested my regex in Java and PHP and it works great, what am i doing wrong ?
Thanks
Try using this regex:
@"<div class=\"fact\" id=\"fact[0-9]*\">([^<]*)</div>"
Regex:
fact[0-9].*
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>
.
来源:https://stackoverflow.com/questions/10522008/parsing-html-nsregularexpression