Skip to content

Instantly share code, notes, and snippets.

@pizthewiz
Created January 28, 2013 19:11
Show Gist options
  • Save pizthewiz/4658164 to your computer and use it in GitHub Desktop.
Save pizthewiz/4658164 to your computer and use it in GitHub Desktop.
NSString category to validate contents as Twitter username.
@interface NSString (CLUTwitterAdditions)
- (BOOL)isValidTwitterUsername;
@end
@implementation NSString (CLUTwitterAdditions)
static NSString* CLUTwitterUsernamePattern = @"^([0-9a-zA-Z_]{1,15})$";
// NB - largely yoinked from http://stackoverflow.com/questions/4424179/twitter-username-regex-validation/4424288#4424288
- (BOOL)isValidTwitterUsername {
static NSRegularExpression* usernameRegularExpression;
if (!usernameRegularExpression) {
usernameRegularExpression = [NSRegularExpression regularExpressionWithPattern:CLUTwitterUsernamePattern options:NSRegularExpressionCaseInsensitive error:nil];
}
NSUInteger numberOfMatches = [usernameRegularExpression numberOfMatchesInString:self options:0 range:NSMakeRange(0, self.length)];
return numberOfMatches == 1;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment