I have a NSString with some html tags, how can I search for tag and get the content of url? I'm not sure if I must use Hpple or a simple Regex expression. In both cases can I have some example?
我有一个带有一些html标签的NSString,我如何搜索标签并获取网址的内容?我不确定我是否必须使用Hpple或简单的Regex表达式。在这两种情况下,我可以举一些例子吗?
1 个解决方案
#1
27
One way to do this is using NSScanner. See this example:
一种方法是使用NSScanner。看这个例子:
NSString *url = nil;
NSString *htmlString = ...
NSScanner *theScanner = [NSScanner scannerWithString:htmlString];
// find start of IMG tag
[theScanner scanUpToString:@"<img" intoString:nil];
if (![theScanner isAtEnd]) {
[theScanner scanUpToString:@"src" intoString:nil];
NSCharacterSet *charset = [NSCharacterSet characterSetWithCharactersInString:@"\"'"];
[theScanner scanUpToCharactersFromSet:charset intoString:nil];
[theScanner scanCharactersFromSet:charset intoString:nil];
[theScanner scanUpToCharactersFromSet:charset intoString:&url];
// "url" now contains the URL of the img
}
#1
27
One way to do this is using NSScanner. See this example:
一种方法是使用NSScanner。看这个例子:
NSString *url = nil;
NSString *htmlString = ...
NSScanner *theScanner = [NSScanner scannerWithString:htmlString];
// find start of IMG tag
[theScanner scanUpToString:@"<img" intoString:nil];
if (![theScanner isAtEnd]) {
[theScanner scanUpToString:@"src" intoString:nil];
NSCharacterSet *charset = [NSCharacterSet characterSetWithCharactersInString:@"\"'"];
[theScanner scanUpToCharactersFromSet:charset intoString:nil];
[theScanner scanCharactersFromSet:charset intoString:nil];
[theScanner scanUpToCharactersFromSet:charset intoString:&url];
// "url" now contains the URL of the img
}