Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9140791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:33:45+00:00 2026-06-17T09:33:45+00:00

If I have a string that returns a value of : <div style=clear:both;></div> <div

  • 0

If I have a string that returns a value of :

<div style="clear:both;"></div>
                <div style="float:left;">
                    <div style="float:left; height:27px; font-size:13px; padding-top:2px;">
                        <div style="float:left;"><a href="http://www.hulkshare.com/ap-nxy2n2wn7ke8.mp3" rel="nofollow" target="_blank" style="color:green;">Download</a></div>

How can I just get the <a href="http://www.hulkshare.com/ap-nxy2n2wn7ke8.mp3" part out of it? I apologise if there is posts about this already, I couldn’t find any.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:33:46+00:00Added an answer on June 17, 2026 at 9:33 am

    Here’s an example of using regular expressions to find substrings. It looks for “href=” and then for the first quote (“) after href=. Once these indexes are found, the string between then is returned.

    Regular expressions aren’t really needed in my example, you could use simple NSString methods to find substrings instead.

    This is just a hard coded example that fits your specific case. In practice you’re better off using a DOM/XML parser to do something like this.

    Also I’m assuming you want to extract the actual URL and don’t care about the

    Also note this function doesn’t handle the case that there is no href match in the string.

    - (NSString *)stringByExtractingAnchorTagURLFromString:(NSString *)dom {
        NSError *error;
    
        // Find the "href=" part
        NSRegularExpression *firstRegexp = [NSRegularExpression regularExpressionWithPattern:@"href=\"" options:NSRegularExpressionCaseInsensitive error:&error];
        NSTextCheckingResult *firstResult = [firstRegexp firstMatchInString:dom options:NSMatchingReportProgress range:NSMakeRange(0, [dom length])];
    
        NSUInteger startIndex = firstResult.range.location + firstResult.range.length;
    
        // Find the first quote (") character after the href=
        NSRegularExpression *secondRegexp = [NSRegularExpression regularExpressionWithPattern:@"\"" options:NSRegularExpressionCaseInsensitive error:&error];
        NSTextCheckingResult *secondResult = [secondRegexp firstMatchInString:dom options:NSMatchingReportProgress range:NSMakeRange(startIndex, [dom length]-startIndex)];
    
        NSUInteger endIndex = secondResult.range.location;
    
        // The URL is the string between these two found locations
        return [dom substringWithRange:NSMakeRange(startIndex, endIndex-startIndex)];
    }
    

    This is how I tested it:

    NSString *dom = @"<div style=\"clear:both;\"></div><div style=\"float:left;\"><div style=\"float:left; height:27px; font-size:13px; padding-top:2px;\"><div style=\"float:left;\"><a href=\"http://www.hulkshare.com/ap-nxy2n2wn7ke8.mp3\" rel=\"nofollow\" target=\"_blank\" style=\"color:green;\">Download</a></div>";
    NSString *result = [self stringByExtractingAnchorTagURLFromString:dom];
    NSLog(@"Result: %@", result);
    

    The test prints:

    Result: http://www.hulkshare.com/ap-nxy2n2wn7ke8.mp3
    

    UPDATE — Multiple HREFs

    For multiple hrefs use this function, which will return an array of NSStrings holding the urls:

    - (NSArray *)anchorTagURLsFromString:(NSString *)dom {
        NSError *error;
        NSMutableArray *urls = [NSMutableArray array];
    
        // First find all matching hrefs in the dom
        NSRegularExpression *firstRegexp = [NSRegularExpression regularExpressionWithPattern:@"href=\"" options:NSRegularExpressionCaseInsensitive error:&error];
        NSArray *matches = [firstRegexp matchesInString:dom options:NSMatchingReportProgress range:NSMakeRange(0, [dom length])];
    
        // Go through all matches and extrac the URL
        for (NSTextCheckingResult *match in matches) {
            NSUInteger startIndex = match.range.location + match.range.length;
    
            // Find the first quote (") character after the href=
            NSRegularExpression *secondRegexp = [NSRegularExpression regularExpressionWithPattern:@"\"" options:NSRegularExpressionCaseInsensitive error:&error];
            NSTextCheckingResult *secondResult = [secondRegexp firstMatchInString:dom options:NSMatchingReportProgress range:NSMakeRange(startIndex, [dom length]-startIndex)];
    
            NSUInteger endIndex = secondResult.range.location;
    
            [urls addObject:[dom substringWithRange:NSMakeRange(startIndex, endIndex-startIndex)]];
        }
    
        return urls;
    }
    

    This is how I tested it:

    NSString *dom2 = @"<div style=\"clear:both;\"></div><div style=\"float:left;\"><div style=\"float:left; height:27px; font-size:13px; padding-top:2px;\"><div style=\"float:left;\"><a href=\"http://www.hulkshare.com/ap-nxy2n2wn7ke8.mp3\" rel=\"nofollow\" target=\"_blank\" style=\"color:green;\">Download</a><a href=\"http://www.google.com/blabla\" rel=\"nofollow\" target=\"_blank\" style=\"color:green;\">Download</a></div>";
    NSArray *urls = [self anchorTagURLsFromString:dom2];
    for (NSString *url in urls) {
        NSLog(@"URL: %@", url);
    }
    

    This is the output of the test:

    URL: http://www.hulkshare.com/ap-nxy2n2wn7ke8.mp3
    URL: http://www.google.com/blabla
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simple web service that returns a string (hardcoded at that).
I have a web service that returns this string via the jQuery $.ajax() call
I have a static method that returns a String, but in the event that
I have a query that returns a string, as well as an escape character
I have a web method that returns a String[][] and I'm trying to parse
I have a method that returns a list of type string. I want to
I have a property in my backing bean that returns html code: public String
I have async methods that returns an objects public static IEnumerable<Users.User> GetUsers(IEnumerable<string> uids, Field
I have a method that returns an IEnumerable<KeyValuePair<string, ArrayList>> , but some of the
I have an interesting problem, which is a function that returns a Dictionary<String,HashSet<String>> .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.