regexp objective-c. html parsing
I need to get some hash variable value from html page content. It looks like this:
//...html code开发者_如何学编程...
var somehash = '12d51e50f4';
//...html code...
How to get value in quotes using Regexp or something else?
This is the regex pattern that would match the specified line of code and extract the hash value into the first capturing group:
\bvar\s+somehash\s*=\s*'([0-9A-F]+)';
@Timur is right in that before asking this type of question you should really read the documentation. That said, here is one way of doing what you ask. You might want to tweak the regular expression for your specific needs. This code was compiled in a commandline tool linking only with the Foundation framework:
int main (int argc, const char * argv[])
{
@autoreleasepool {
NSString *html = @"<html>\n<head>\n<title>Test</title>\n</head>\n<body>var someHash = '123abc';</body></html>";
NSRegularExpression *regexp = [NSRegularExpression regularExpressionWithPattern:@"var someHash = '(\\w*)';" options:NSRegularExpressionCaseInsensitive error:NULL];
NSTextCheckingResult *match = [regexp firstMatchInString:html options:0 range:NSMakeRange(0, html.length)];
if (match) {
NSRange hashRange = [match rangeAtIndex:1];
NSString *hashCode = [html substringWithRange:hashRange];
NSLog(@"Hash Code is %@", hashCode);
}
}
return 0;
}
For production code you will want to check for errors.
精彩评论