Extract Contents of Anchor Tag

2019-09-02 16:15发布

问题:

What I'm trying to do is extract the contents of a an anchor tag being stored in an NSString.

If for example I have a string with the following:

<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>

How would I go about extracting the contents of the anchor tag so that I would have the following:

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

Any help would be greatly appreciated!

I'm completely stumped, whereas this should be quite simple? The answer posted below keeps returning null.

回答1:

If you can require Lion, then you can use 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);
}


回答2:

One Possible solution is by using 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);

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