views:

27

answers:

2

HI,

I have a NSString that cotains a lot of text. Inside the text is a iTunes URL. The URL is masked with BBCode. How can I extract the plain URL?

Sorry, but my regex skills are really bad.

The text:

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At ver [itunes link="http://www.example.tld/redirect.php?url=http%3A%2F%2Fitunes.apple.com%2Fde%2Fapp%2Fbaby-monitor-alarm%2Fid331519989%3Fmt%3D8%26uo%3D4" title="Babyphon"] Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At ver

My First Try:

NSString *regEx = @"(link=?).*"; 
NSString *match = [myText stringByMatching:regEx]; 

if ([match isEqual:@""] == NO) { 
    NSLog(@"Match Ituneslink; %@", match); 
}
else
{ 
    NSLog(@"Not found."); 
}

The result:

link="http://www.appsnight.tv/redirect.php?url=http%3A%2F%2Fitunes.apple.com%2Fde%2Fapp%2Fmare-fernweh%2Fid379051618%3Fmt%3D8%26uo%3D4" title="Mare"]

It takes too much, I need only the itunes link :-(

A: 

If the scheme is always the same, you could proceed like this:

NSRange begin = [myText rangeOfString:@"link="];
NSRange end = [myText rangeOfString:@" title="];

Use the location components of the two ranges to create a new one:

NSRange linkRange;
linkRange.location = begin.location + 6;
linkRange.length = (end.location - 1) - (linkRange.location);

With which you can do:

NSString *myLink = [myText substringWithRange:linkRange];

I have not tried it with your example text, maybe I got the offsets (+6 and -1) wrong and you need to adjust them. Hope that helps!

Toastor
A: 

I found this solution.

 NSString *regEx = @"(link=\"?)[a-zA-Z0-9:/.?=%-]*";
 NSString *match = [myText stringByMatching:regEx]; 
Raphael