valbu17
valbu17

Reputation: 4124

Get a clean Youtube link from a NSString

I have stored in a NSString a youtube link but it's store in this way

<iframe width=""420"" height=""315"" src=""//www.youtube.com/watch?v=IeLZAYz5KpE"" frameborder=""0"" allowfullscreen></iframe>

But, I would like to transform it or get or change to just the link like //www.youtube.com/watch?v=IeLZAYz5KpE to be able to display it in a video player within my app.

I don't have any idea how can I accomplish this.

Update Code to test it this with the answer shown below..

NSString *searchedString = myURL;
NSRange   searchedRange = NSMakeRange(0, [searchedString length]);
NSString *pattern = @"\"\"(\\/\\/.*?)\"\"";
NSError  *error = nil;

NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];
NSTextCheckingResult *match = [regex firstMatchInString:searchedString options:0 range: searchedRange];
NSLog(@"group1: %@", [searchedString substringWithRange:[match rangeAtIndex:1]]);

Cheers!

Upvotes: 1

Views: 208

Answers (1)

Federico Piazza
Federico Piazza

Reputation: 30995

If you want to get the youtube link with from that string, you can use this regex:

""(//.*?)""

Working demo

enter image description here

MATCH 1
1.  [43-80] `//www.youtube.com/watch?v=IeLZAYz5KpE`

Update: to use my regex on objective-c you need to escape special characters as following:

\"\"(\\/\\/.*?)\"\"

Upvotes: 2

Related Questions