arsenius
arsenius

Reputation: 13286

How to use this regex with NSRegularExpression?

I'm trying to extract the youtube video id from a URL using the regex from this answer. However I can't figure out how to format it correctly to work with NSRegularExpression. I have tried escaping the backslashes for C, as well as using escapedTemplateForString and escapedPatternForString. I also tried adding a backslash before the opening/closing brackets. Each case returns NSNotFound for all URLs I try.

// Original:  /^.*(?:(?:youtu\.be\/|v\/|vi\/|u\/\w\/|embed\/)|(?:(?:watch)?\?v(?:i)?=|\&v(?:i)?=))([^#\&\?]*).*/
NSString *c_escaped = @"/^.*(youtu.be\\/|v\\/|e\\/|u\\/\\w+\\/|embed\\/|v=)([^#\\&\\?]*).*/";
NSString *template  = [NSRegularExpression escapedTemplateForString:c_escaped]; // "/^.*(youtu.be\\/|v\\/|e\\/|u\\/\\w+\\/|embed\\/|v=)([^#\\&\\?]*).*/"
NSString *pattern   = [NSRegularExpression escapedPatternForString:c_escaped];  // "\/\^\.\*\(youtu\.be\\\/\|v\\\/\|e\\\/\|u\\\/\\w\+\\\/\|embed\\\/\|v=\)\(\[\^#\\&\\\?]\*\)\.\*\/"

NSRegularExpression *expr = [NSRegularExpression regularExpressionWithPattern:c_escaped
                                                                      options:0
                                                                        error:&error];
NSRange range = [expr rangeOfFirstMatchInString:self options:0 range:NSMakeRange(0, self.length)];

NSRegularExpression *expr1 = [NSRegularExpression regularExpressionWithPattern:template
                                                                      options:0
                                                                        error:&error];
NSRange range1 = [expr1 rangeOfFirstMatchInString:self options:0 range:NSMakeRange(0, self.length)];

NSRegularExpression *expr2 = [NSRegularExpression regularExpressionWithPattern:pattern
                                                                      options:0
                                                                        error:&error];
NSRange range2 = [expr2 rangeOfFirstMatchInString:self options:0 range:NSMakeRange(0, self.length)];

These are the URLs I've tested against:

 NSArray *urls = @[
                  @"//www.youtube-nocookie.com/embed/up_lNV-yoK4?rel=0",
                  @"http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo",
                  @"http://www.youtube.com/watch?v=cKZDdG9FTKY&feature=channel",
                  @"http://www.youtube.com/watch?v=yZ-K7nCVnBI&playnext_from=TL&videos=osPknwzXEas&feature=sub",
                  @"http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I",
                  @"http://www.youtube.com/user/SilkRoadTheatre#p/a/u/2/6dwqZw0j_jY",
                  @"http://youtu.be/6dwqZw0j_jY",
                  @"http://www.youtube.com/watch?v=6dwqZw0j_jY&feature=youtu.be",
                  @"http://youtu.be/afa-5HQHiAs",
                  @"http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo?rel=0",
                  @"http://www.youtube.com/watch?v=cKZDdG9FTKY&feature=channel",
                  @"http://www.youtube.com/watch?v=yZ-K7nCVnBI&playnext_from=TL&videos=osPknwzXEas&feature=sub",
                  @"http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I",
                  @"http://www.youtube.com/embed/nas1rJpm7wY?rel=0",
                  @"http://www.youtube.com/watch?v=peFZbP64dsU",
                  @"http://youtube.com/v/dQw4w9WgXcQ?feature=youtube_gdata_player",
                  @"http://youtube.com/vi/dQw4w9WgXcQ?feature=youtube_gdata_player",
                  @"http://youtube.com/?v=dQw4w9WgXcQ&feature=youtube_gdata_player",
                  @"http://www.youtube.com/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player",
                  @"http://youtube.com/?vi=dQw4w9WgXcQ&feature=youtube_gdata_player",
                  @"http://youtube.com/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player",
                  @"http://youtube.com/watch?vi=dQw4w9WgXcQ&feature=youtube_gdata_player",
                  @"http://youtu.be/dQw4w9WgXcQ?feature=youtube_gdata_player"];

Upvotes: 1

Views: 601

Answers (1)

David Gish
David Gish

Reputation: 750

You need to delete the leading and trailing slashes. The problem is, you're adapting this from JavaScript which allows the use of "/" to delimit strings. You also need to escape the backslashes to make the Obj-C compiler do the right thing, but that's all. Try this:

@implementation NSString (youtube)

- (BOOL)isYouTubeURL
{
    NSString *youtubePattern = @"^.*(?:(?:youtu\\.be\\/|v\\/|vi\\/|u\\/\\w\\/|embed\\/)|(?:(?:watch)?\\?v(?:i)?=|\\&v(?:i)?=))([^#\\&\\?]*).*";

    NSRegularExpression *expr = [NSRegularExpression regularExpressionWithPattern:youtubePattern
                                                                          options:0
                                                                            error:nil];
    NSRange range = [expr rangeOfFirstMatchInString:self options:0 range:NSMakeRange(0, self.length)];
    return range.location != NSNotFound;
}

@end

Upvotes: 2

Related Questions