24

I have a Youtube url as an NSString or Swift String, but I need to extract the video id that is displayed in the url. I found many tutorials on how to do this in php or and other web-based programming languages, but none in Objective-C or Swift for Apple platforms...

I'm looking for a method that asks for an NSString url as the parameter and returns the video id as another NSString...

pkamb
  • 33,281
  • 23
  • 160
  • 191
The Man
  • 1,462
  • 3
  • 23
  • 45

18 Answers18

71

Here is RegExp it cover these cases

Objective C

- (NSString *)extractYoutubeIdFromLink:(NSString *)link {
    NSString *regexString = @"((?<=(v|V)/)|(?<=be/)|(?<=(\\?|\\&)v=)|(?<=embed/))([\\w-]++)";
    NSRegularExpression *regExp = [NSRegularExpression regularExpressionWithPattern:regexString
                                                                            options:NSRegularExpressionCaseInsensitive
                                                                              error:nil];

    NSArray *array = [regExp matchesInString:link options:0 range:NSMakeRange(0,link.length)];
    if (array.count > 0) {
        NSTextCheckingResult *result = array.firstObject;
        return [link substringWithRange:result.range];
    }
    return nil;
}

Swift

func extractYoutubeIdFromLink(link: String) -> String? {
    let pattern = "((?<=(v|V)/)|(?<=be/)|(?<=(\\?|\\&)v=)|(?<=embed/))([\\w-]++)"
    guard let regExp = try? NSRegularExpression(pattern: pattern, options: .CaseInsensitive) else {
        return nil
    }
    let nsLink = link as NSString
    let options = NSMatchingOptions(rawValue: 0)
    let range = NSRange(location: 0,length: nsLink.length)
    let matches = regExp.matchesInString(link as String, options:options, range:range)
    if let firstMatch = matches.first {
        return nsLink.substringWithRange(firstMatch.range)
    }
    return nil
}

Swift 3

func extractYoutubeIdFromLink(link: String) -> String? {
    let pattern = "((?<=(v|V)/)|(?<=be/)|(?<=(\\?|\\&)v=)|(?<=embed/))([\\w-]++)"
    guard let regExp = try? NSRegularExpression(pattern: pattern, options: .caseInsensitive) else {
        return nil
    }
    let nsLink = link as NSString
    let options = NSRegularExpression.MatchingOptions(rawValue: 0)
    let range = NSRange(location: 0, length: nsLink.length)
    let matches = regExp.matches(in: link as String, options:options, range:range)
    if let firstMatch = matches.first {
        return nsLink.substring(with: firstMatch.range)
    }
    return nil
}
Alex
  • 1,603
  • 1
  • 16
  • 11
  • @AlmasAdilbek, it's just an example, it is working and you can change it static or convert to String extension, it's up to you – Alex Sep 22 '16 at 09:46
  • there is still some case where this regex is not working so here is the new regex pattern use this.@"(?<=watch\\?v=|/videos/|embed\\/|youtu.be\\/|\\/v\\/|\\/e\\/|watch\\?v%3D|watch\\?feature=player_embedded&v=|%2Fvideos%2F|embed%\u200C\u200B2F|youtu.be%2F|%2Fv%2F)[^#\\&\\?\\n]*" – Ravi Panchal Feb 02 '17 at 09:58
  • @ravi.p can you give me an example when this regex is not working? – Alex Feb 05 '17 at 14:48
39

After spending ages trying to find the correct syntax for the regex, I've come across this which has helped me.

NSString *regexString = @"(?<=v(=|/))([-a-zA-Z0-9_]+)|(?<=youtu.be/)([-a-zA-Z0-9_]+)";

Taken from here. This works for the following URL formats:

 - www.youtube.com/v/VIDEOID  
 - www.youtube.com?v=VIDEOID
 - http://www.youtube.com/watch?v=KFPtWedl7wg&feature=youtu.be
 - http://www.youtube.com/watch?v=MkTD2Y4LXcM 
 - youtu.be/KFPtWedl7wg_U923
 - http://www.youtube.com/watch?feature=player_detailpage&v=biVLGTAMC_U#t=31s
Vega
  • 27,856
  • 27
  • 95
  • 103
jt_uk
  • 1,362
  • 1
  • 15
  • 17
  • 3
    This worked for me, just to mention that uou ned to access to the range number 0 to get it: NSRange videoIDRange = [match rangeAtIndex:0]; NSString *videoID = [message substringWithRange:videoIDRange]; – João Nunes Feb 22 '13 at 11:56
  • 1
    Dis be da cooolest answer evaaa! If it was combined with dat Dima homie's answer – GangstaGraham Jun 19 '13 at 04:16
15

So a YouTube URL looks something like:

http://www.youtube.com/watch?v=oHg5SJYRHA0

The video ID you're interested in is the part at the end (oHg5SJYRHA0).... though it's not necessarily at the end, as YouTube URLs can contain other parameters in the query string.

Your best bet is probably to use a regular expression and Foundation's NSRegularExpression class. I'd presume this approach is used in the other-language tutorials you've found -- note that the content of regular expressions is pretty much the same in any language or toolkit which includes them, so any regex found in those tutorials should work for you. (I'd advise against your approach of breaking on v= and taking exactly 11 characters, as this is prone to various modes of failure to which a regex is more robust.)

To find the video ID you might want a regex like v=([^&]+). The v= gets us to the right part of the query URL (in case we get something like watch?fmt=22&v=oHg5SJYRHA0). The parentheses make a capture group so we can extract only the video ID and not the other matched characters we used to find it, and inside the parentheses we look for a sequence of one or more characters which is not an ampersand -- this makes sure we get everything in the v=whatever field, and no fields after it if you get a URL like watch?v=oHg5SJYRHA0&rel=0.

Whether you use this or another regex, it's likely that you'll be using capture groups. (If not, rangeOfFirstMatchInString:options:range: is just about all you need, as seen in Dima's answer.) You can get at the contents of capture groups (as NSTextCheckingResult objects) using firstMatchInString:options:range: or similar methods:

NSError *error = NULL;
NSRegularExpression *regex = 
[NSRegularExpression regularExpressionWithPattern:@"?.*v=([^&]+)"
                                          options:NSRegularExpressionCaseInsensitive
                                            error:&error];
NSTextCheckingResult *match = [regex firstMatchInString:youtubeURL
                                                options:0
                                                  range:NSMakeRange(0, [youtubeURL length])];
if (match) {
    NSRange videoIDRange = [match rangeAtIndex:1];
    NSString *substringForFirstMatch = [youtubeURL substringWithRange:videoIDRange];
}
Vega
  • 27,856
  • 27
  • 95
  • 103
rickster
  • 124,678
  • 26
  • 272
  • 326
9

The tutorials you are probably seeing are just instructions on how to use regular expressions, which is also what you want to use in this case.

The Cocoa class you will need to use is NSRegularExpression.

Your actual regex string will depend on the format you are expecting the url to be in since it looks like youtube has several. The general function will look something like:

+ (NSString *)extractYoutubeID:(NSString *)youtubeURL
{
  NSError *error = NULL;  
  NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"your regex string goes here" options:NSRegularExpressionCaseInsensitive error:&error];
  NSRange rangeOfFirstMatch = [regex rangeOfFirstMatchInString:youtubeURL options:0 range:NSMakeRange(0, [youtubeURL length])];
  if(!NSEqualRanges(rangeOfFirstMatch, NSMakeRange(NSNotFound, 0)))
  {
    NSString *substringForFirstMatch = [youtubeURL substringWithRange:rangeOfFirstMatch];

    return substringForFirstMatch;
  }
  return nil;
}
Dima
  • 23,484
  • 6
  • 56
  • 83
7

You don't even need regexes. The following works regardless of the length of the video ID and its position within the URL:

NSString *vID = nil;
NSString *url = @"http://www.youtube.com/watch?v=cAcqdjLCN7s";

NSString *query = [url componentsSeparatedByString:@"?"][1];
NSArray *pairs = [query componentsSeparatedByString:@"&"];
for (NSString *pair in pairs) {
    NSArray *kv = [pair componentsSeparatedByString:@"="];
    if ([kv[0] isEqualToString:@"v"]) {
        vID = kv[1];
        break;
    }
}

NSLog(@"%@", vID);
  • Much nicer than regular expressions. Still doesn’t take [this](http://stackoverflow.com/questions/11509164/youtube-video-id-from-url-objective-c/17798344#comment15209148_11509927) into account like youtube-dl does, but oh well. ;) – Ry- Jul 23 '13 at 19:46
  • @minitech My principle: if we're developing buggy hacks, then we should at least be elegant :P Thanks. (By the way: if I understand your point correctly, my error is easily solvable by separatimg the URL by characters in the `?#` character set...) –  Jul 23 '13 at 19:50
3

Based on this answer: PHP Regex to get youtube video ID?

I adapted a regex for c/objc/c++ string, the important part here is that the regex doesn't get videos from facebook or other services. iOS regex is based on: ICU

NSString *regexString = @"^(?:http(?:s)?://)?(?:www\\.)?(?:m\\.)?(?:youtu\\.be/|youtube\\.com/(?:(?:watch)?\\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)/))([^\?&\"'>]+)";

NSError *error;
NSRegularExpression *regex =
[NSRegularExpression regularExpressionWithPattern:regexString
                                          options:NSRegularExpressionCaseInsensitive
                                            error:&error];
NSTextCheckingResult *match = [regex firstMatchInString:message
                                                options:0
                                                  range:NSMakeRange(0, [message length])];

if (match && match.numberOfRanges == 2) {
    NSRange videoIDRange = [match rangeAtIndex:1];
    NSString *videoID = [message substringWithRange:videoIDRange];
    
    return videoID;
}

Matches:

- youtube.com/v/vidid
- youtube.com/vi/vidid
- youtube.com/?v=vidid
- youtube.com/?vi=vidid
- youtube.com/watch?v=vidid
- youtube.com/watch?vi=vidid
- youtu.be/vidid
- youtube.com/embed/vidid
- http://youtube.com/v/vidid
- http://www.youtube.com/v/vidid
- https://www.youtube.com/v/vidid
- youtube.com/watch?v=vidid&wtv=wtv
- http://www.youtube.com/watch?dev=inprogress&v=vidid&feature=related
- https://m.youtube.com/watch?v=vidid

Does not match:

- www.facebook.com?wtv=youtube.com/v/vidid
- https://www.facebook.com/video.php?v=10155279523025107
Vega
  • 27,856
  • 27
  • 95
  • 103
João Nunes
  • 3,751
  • 31
  • 32
2
- (NSString*)getYoutubeVideoID:(NSString*)url {
    NSError *error = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?<=watch\\?v=|/videos/|embed\\/)[^#\\&\\?]*"
                                              options:NSRegularExpressionCaseInsensitive
                                                error:&error];

    NSTextCheckingResult *match = [regex firstMatchInString:url
                                                    options:0
                                                      range:NSMakeRange(0, [url length])];
    NSString *substringForFirstMatch;
    if (match) {
        NSRange videoIDRange = [match rangeAtIndex:0];
        substringForFirstMatch = [url substringWithRange:videoIDRange];
    }
    return substringForFirstMatch;
}
Krishna Raj Salim
  • 7,331
  • 5
  • 34
  • 66
2

I used the highest voted answer to write a better, more restrictive regex.

NSString *regex = @"(?:youtube.com.+v[=/]|youtu.be/)([-a-zA-Z0-9_]+)";

and then you can get the ID

NSTextCheckingResult *match = [regex firstMatchInString:url
                                                options:0
                                                  range:NSMakeRange(0, [url length])];
NSRange videoIDRange = [match rangeAtIndex:1];
NSString *youTubeID = [url substringWithRange:videoIDRange];
skensell
  • 1,421
  • 12
  • 21
2

Swift 5

Here is the latest working version I am using. I've added support for YouTube short videos as well. Ex: https://youtube.com/shorts/2xL2WlQM7Nc

extension String{
  func extractYoutubeId() -> String? {
    let pattern = "((?<=(v|V)/)|(?<=be/)|(?<=(\\?|\\&)v=)|(?<=embed/)|(?<=shorts/))([\\w-]++)"
    if let matchRange = self.range(of: pattern, options: .regularExpression) {
        return String(self[matchRange])
    } else {
        return .none
    }
  }
}

Example:

let ytShortVideoLink = "https://youtube.com/shorts/2xL2WlQM7Nc"
print("Video ID:",ytShortVideoLink. extractYoutubeId())
//Output: 
Video ID: 2xL2WlQM7Nc
iRiziya
  • 3,235
  • 1
  • 22
  • 36
1

Here is my solution which referenced from StackOveFlow. (Youtube I.D parsing for new URL formats)

I did some modification.

///This is the .h

#import <Foundation/Foundation.h>
@interface YoutubeParser : NSObject

+(BOOL) isValidateYoutubeURL:(NSString * )youtubeURL;

+(NSArray *) parseHTML:(NSString *)html ;

@end

///This is the .m 
#import "YoutubeParser.h"

@interface YoutubeParser () {
}

@end

@implementation YoutubeParser

#define YOUTUBE_PATTERN @"(https?://)?(www\\.)?(youtu\\.be/|youtube\\.com)?(/|/embed/|/v/|/watch\\?v=|/watch\\?.+&v=)([\\w_-]{11})(&.+)?"

+(NSRegularExpression *)regex {

    static NSRegularExpression * regex = nil;

    regex =     [NSRegularExpression regularExpressionWithPattern:YOUTUBE_PATTERN
                                                               options:NSRegularExpressionCaseInsensitive
                                                                 error:nil];
    return regex;
}

+(BOOL) isValidateYoutubeURL:(NSString * )youtubeURL {
    NSInteger cnt = [[YoutubeParser regex] numberOfMatchesInString:youtubeURL options:0 range:NSMakeRange(0, [youtubeURL length])  ];

    return cnt > 0 ? YES : NO;
}

typedef void (^matching_block_t)  (NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop);

+(NSArray *) parseHTML:(NSString *)html {
    NSMutableArray * youtubeURLArray = [[NSMutableArray alloc] init];

    matching_block_t parseTask = ^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
        NSRange matchRange = [result range];
        NSRange youtubeKey = [result rangeAtIndex:5]; //the youtube key
        NSString * strKey = [html substringWithRange:youtubeKey] ;
        NSLog(@"youtubeKey=%@ , with url=%@ " ,strKey , [html substringWithRange:matchRange]);

        [youtubeURLArray addObject:strKey];
    };

    [[YoutubeParser regex] enumerateMatchesInString:html   options:0   range:NSMakeRange(0, [html length])   usingBlock:parseTask ];

    return youtubeURLArray;
}

@end
Community
  • 1
  • 1
Prophecy
  • 11
  • 2
1

There is a lot of good answers here but I thought it might be beneficial to some to parse multiple video_ID from a string. This could be a web page or an array of different URL.

Example of page content

NSString *content = @"http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo,http://youtu.be/NLqAF9hrVbY,http://www.youtube.com/watch?v=NLqAF9hrVbY,http://facebook.com,http://www.youtube.com/watch?v=cAcqdjLCN7s";

Method

-(NSArray *)extractVideos:(NSString *)content {
    NSString *extractRegex = @"(?<=v(=|/))([-a-zA-Z0-9_]+)|(?<=youtu.be/)([-a-zA-Z0-9_]+)"
    NSMutableArray *extractedContent = [[NSMutableArray alloc] init];
    if ([content hasPrefix:@"http://"] || [content hasPrefix:@"https://"]) {
        NSURL *extractURL = [NSURL URLWithString:content];
        if ([extractURL.host rangeOfString:@"youtu"].location != NSNotFound) {
            NSRegularExpression *extractRegex = [NSRegularExpression regularExpressionWithPattern:extractRegex options:NSRegularExpressionCaseInsensitive error:nil];
            NSArray *extractResults = [extractRegex matchesInString:content options:0 range:NSMakeRange(0, content.length)];
            for (NSTextCheckingResult *match in extractResults) {
                [extractedContent addObject:[content substringWithRange:match.range]];

            }

        }

    }

    return extractedContent;

}

Output

( 
    NLqAF9hrVbY,
    QLqAF9eeVbY,
    cAcqdjLCN7s
)

Credit to @Alex for the Regex

Joe Barbour
  • 842
  • 9
  • 14
  • Thank you for detailed answer, but I haven't been able to figure out what's YT_ID_EXTRACT_REGEX – Rimon Jun 28 '15 at 07:29
  • Never mind, figured it out. For others who were wondering too, just add `#define YT_ID_EXTRACT_REGEX @"(?<=v(=|/))([-a-zA-Z0-9_]+)|(?<=youtu.be/)([-a-zA-Z0-9_]+)"` to the top of your file instead of this line 'NSString *extractRegex = @"(?<=v(=|/))([-a-zA-Z0-9_]+)|(?<=youtu.be/)([-a-zA-Z0-9_]+)"' – Rimon Jun 28 '15 at 07:33
  • @RimonRagaie sorry for the error in the code there, I've changed it now but a little too late. Hope the rest of the answer helped. – Joe Barbour Jun 28 '15 at 17:05
1

Merging some of your answers, I'd say this is the best answer:

+ (NSString *)extractYoutubeID:(NSString *)youtubeURL
{
    NSError *error = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?<=v(=|/))([-a-zA-Z0-9_]+)|(?<=youtu.be/)([-a-zA-Z0-9_]+)"
                                                                           options:NSRegularExpressionCaseInsensitive
                                                                             error:&error];
    NSRange rangeOfFirstMatch = [regex rangeOfFirstMatchInString:youtubeURL options:NSMatchingReportProgress range:NSMakeRange(0, [youtubeURL length])];
    if(rangeOfFirstMatch.location != NSNotFound) {
        NSString *substringForFirstMatch = [youtubeURL substringWithRange:rangeOfFirstMatch];

        return substringForFirstMatch;
    }

    return nil;
}
jomafer
  • 2,655
  • 1
  • 32
  • 47
1

Swift 2 version for @Alex answer

func getYoutubeVideoId(youtubeLink:String) -> String?{
    var youtubeId:String? = nil
    let pattern: String = "((?<=(v|V)/)|(?<=be/)|(?<=(\\?|\\&)v=)|(?<=embed/))([\\w-]++)"
    do {
        let regex = try NSRegularExpression(pattern: pattern, options: .CaseInsensitive)
        if let regexMatch = regex.firstMatchInString(youtubeLink, options: NSMatchingOptions(rawValue: 0), range: NSRange(location: 0, length: youtubeLink.characters.count)) {
            youtubeId = (youtubeLink as NSString).substringWithRange(regexMatch.range)
        }
    }
    catch let error as NSError{
        print("Error while extracting youtube id \(error.debugDescription)")
    }

    return youtubeId
}
Sunil Sharma
  • 2,653
  • 1
  • 25
  • 36
1

Update Swift 4:

static func extractYoutubeVideoId(from url: String) -> String? {
    let pattern = "((?<=(v|V)/)|(?<=be/)|(?<=(\\?|\\&)v=)|(?<=embed/))([\\w-]++)"
    guard let range = url.range(of: pattern, options: .regularExpression) else { return nil }
    return String(url[range])
}

Old answer:
A bit swifter way of @Alex's Swift 3 answer w/o the use of NSString:
We can force try the regex, because we know it is valid.

static func extractYoutubeVideoId(from url: String) -> String? {
    let pattern = "((?<=(v|V)/)|(?<=be/)|(?<=(\\?|\\&)v=)|(?<=embed/))([\\w-]++)"
    let regex = try! NSRegularExpression(pattern: pattern, options: [.caseInsensitive])
    let range = NSRange(location: 0, length: url.utf16.count)
    guard let firstMatch = regex.firstMatch(in: url, options: .init(rawValue: 0), range: range) else { return nil }
    let start = String.UTF16Index(firstMatch.range.location)
    let end = String.UTF16Index(firstMatch.range.location + firstMatch.range.length)
    return String(url.utf16[start..<end])
}

Or, if you still want NSString:

static func extractYoutubeVideoId(from url: String) -> String? {
    let pattern = "((?<=(v|V)/)|(?<=be/)|(?<=(\\?|\\&)v=)|(?<=embed/))([\\w-]++)"
    let regex = try! NSRegularExpression(pattern: pattern, options: [.caseInsensitive])
    let range = NSRange(location: 0, length: (url as NSString).length)
    guard let firstMatch = regex.firstMatch(in: url, options: .init(rawValue: 0), range: range) else { return nil }
    return (url as NSString).substring(with: firstMatch.range)
}
ElegyD
  • 4,393
  • 3
  • 21
  • 37
0

Here is the swift version using @jt_ik's regex:

func extractYoutubeID(youtubeURL: String) -> String {
    var error: NSError?
    let pattern: String = "(?<=v(=|/))([-a-zA-Z0-9_]+)|(?<=youtu.be/)([-a-zA-Z0-9_]+)"
    let regex = NSRegularExpression(pattern: pattern, options: .CaseInsensitive, error: &error)!

    if error == nil {
        if let regexMatch = regex.firstMatchInString(youtubeURL, options: nil, range: NSRange(location: 0, length: youtubeURL.utf16Count)) {
            return (youtubeURL as NSString).substringWithRange(regexMatch.range)
        }
        // Handle no match here
        return ""
    } else {
        // Handle error here
        println(error?.userInfo)
        return ""
    }
}
benaneesh
  • 426
  • 6
  • 10
0

Swift 3 version for @Alex answer

func extractYoutubeIdFromLink(link: String) -> String? {
    let pattern = "((?<=(v|V)/)|(?<=be/)|(?<=(\\?|\\&)v=)|(?<=embed/))([\\w-]++)"
    guard let regExp = try? NSRegularExpression(pattern: pattern, options: .caseInsensitive) else {
        return nil
    }
    let nsLink = link as NSString
    let options = NSRegularExpression.MatchingOptions(rawValue: 0)
    let range = NSRange(location: 0,length: nsLink.length)
    let matches = regExp.matches(in: link as String, options:options, range:range)
    if let firstMatch = matches.first {
        debugPrint(firstMatch)
        return nsLink.substring(with: firstMatch.range)
    }
    return nil
}
ddnl
  • 495
  • 1
  • 6
  • 22
0

In Swift 5.1, I created string extension.

func returnYoutubeIDFromURL() -> String? {
    
   let pattern = #"^(?:https?:\/\/)?(?:(?:www\.)?youtube\.com\/(?:(?:v\/)|(?:embed\/|watch(?:\/|\?)){1,2}(?:.*v=)?|.*v=)?|(?:www\.)?youtu\.be\/)([A-Za-z0-9_\-]+)&?.*$"#
 
    let regex = try? NSRegularExpression(pattern: pattern, options: [.caseInsensitive])
    let range = NSRange(location: 0, length: self.utf16.count)
    if let regexMatch = regex?.firstMatch(in: self, options: [], range: range) {
      let videoId = (self as NSString).substring(with: regexMatch.range(at: 1))
      print(videoId)
      return videoId
    }
    return nil
  }

Usage: "https://youtu.be/YT_ID".returnYoutubeIDFromURL() will return YT_ID

byJeevan
  • 3,728
  • 3
  • 37
  • 60
-2

I figured it out by myself...

NSArray *videoURLSplit = [videoURL componentsSeparatedByString:@"v="];
NSString *videoID = [[videoURLSplit objectAtIndex:1] substringToIndex:11];
NSLog(@"%@",videoID);

Very simple... All video urls contain v=VIDEO_ID I just separated the url into an array and then took the first 11 digits of what is after v= since there could be more http GET information in the url...

Thanks for helping though!

The Man
  • 1,462
  • 3
  • 23
  • 45
  • 1
    Be careful with this because that is actually not true. There are several other formats the videos can come in such as `http://youtu.be/###########` and `youtube.com/v/###########`. – Dima Jul 16 '12 at 18:08
  • 1
    And what happens when youtube begins using IDs longer than 11 characters. You need to consider this as well. – eternalmatt Jul 16 '12 at 18:11
  • @Dima Thanks for that, but I load http://www.youtube.com/watch?nomobile=1 this specific address in a webview and I only allow the user to search for videos and click on them so just about everytime the url should be... http://www.youtube.com/v=VIDEO_ID – The Man Jul 16 '12 at 18:12
  • @eternalmatt Check out this on SO... http://stackoverflow.com/questions/6180138/whats-the-youtube-video-id-maximum-length – The Man Jul 16 '12 at 18:12
  • @TheMan, then your solution should be fine. Keep in mind that these are exactly the sort of details you should be including in the question. Otherwise, people will not be able to help you come to the correct answer. – Dima Jul 16 '12 at 18:15
  • Mathematically, there's no reason to use more than 11 characters, but what if they just feel like using 10 or 12 one day? – eternalmatt Jul 16 '12 at 18:15
  • @eternalmatt I wouldn't see why that would randomly change it to 11 or 12 characters? – The Man Jul 16 '12 at 18:17