Created
January 28, 2013 19:11
-
-
Save pizthewiz/4658164 to your computer and use it in GitHub Desktop.
NSString category to validate contents as Twitter username.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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