提取锚标记的内容

时间:2012-04-24 16:48:08

标签: objective-c xcode cocoa webview

我要做的是提取存储在NSString中的锚标记的内容。

例如,如果我有一个包含以下内容的字符串:

<a href="/url?q=https://kindle.amazon.com/&amp;sa=U&amp;ei=GdiWT5uCEI6BhQfihoTzDQ&amp;ved=0CCUQFjAB&amp;usg=AFQjCNEoRolsgoynLNS0H60VWz-9EaQdtw">Amazon <b>Kindle</b>: Welcome</a>

我将如何提取锚标记的内容,以便我拥有以下内容:

https://kindle.amazon.com/&amp;sa=U&amp;ei=GdiWT5uCEI6BhQfihoTzDQ&amp;ved=0CCUQFjAB&amp;usg=AFQjCNEoRolsgoynLNS0H60VWz-9EaQdtw

非常感谢任何帮助!

我完全被难倒了,而这应该很简单?下面发布的答案会一直返回null。

2 个答案:

答案 0 :(得分:0)

一种可能的解决方案是使用NSScanner -

NSString *urlString = nil;
NSString *htmlString = @"<a href=\"/url?q=https://kindle.amazon.com/&amp;sa=U&amp;ei=GdiWT5uCEI6BhQfihoTzDQ&amp;ved=0CCUQFjAB&amp;usg=AFQjCNEoRolsgoynLNS0H60VWz-9EaQdtw\">Amazon <b>Kindle</b>: Welcome</a>";

NSScanner *scanner = [NSScanner scannerWithString:htmlString];

[scanner scanUpToString:@"<a" intoString:nil];
if (![scanner isAtEnd]) {
    [scanner scanUpToString:@"http" intoString:nil];
    NSCharacterSet *charset = [NSCharacterSet characterSetWithCharactersInString:@">"];
    [scanner scanUpToCharactersFromSet:charset intoString:&urlString];
}
NSLog(@"%@", urlString);

在日志中 - https://kindle.amazon.com/&sa=U&ei=GdiWT5uCEI6BhQfihoTzDQ&ved=0CCUQFjAB&usg=AFQjCNEoRolsgoynLNS0H60VWz-9EaQdtw

答案 1 :(得分:0)

如果您可以要求Lion,则可以使用NSRegularExpression

NSString* stringToSearch = @"<a href=\"/url?q=https://kindle.amazon.com/&amp;sa=U&amp;ei=GdiWT5uCEI6BhQfihoTzDQ&amp;ved=0CCUQFjAB&amp;usg=AFQjCNEoRolsgoynLNS0H60VWz-9EaQdtw\">Amazon <b>Kindle</b>: Welcome</a>";

NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"href\\s*=\\s*\"\\/url\\?q=([^\"]*)\""
                                                                       options:NSRegularExpressionCaseInsensitive
                                                                         error:&error];

NSTextCheckingResult* match = [regex firstMatchInString:stringToSearch options:0 range:NSMakeRange(0, [stringToSearch length])];
if(match.numberOfRanges == 2)
{
    NSRange capture = [match rangeAtIndex:1];
    NSString* URLString = [stringToSearch substringWithRange:capture];
    NSLog(@"%@",URLString);
}