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.
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.
This is how I tested it:
The test prints:
UPDATE — Multiple HREFs
For multiple hrefs use this function, which will return an array of NSStrings holding the urls:
This is how I tested it:
This is the output of the test: