iOS NSRegularExpression to detect UUID
In keeping with my attempt to use more regular expressions in .NET, I figured it would be a good exercise to try and use NSRegularExpression to do some checking in my iOS app.
I have a situation where an array of strings come in, and I need to know which of them are UUIDs and which are not. Here is the code that I wrote to accomplish the UUID checking, it is implemented as a category of NSString:
#define UUID_PATTERN @"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$" - (BOOL)isUUID { NSRegularExpression *regex; regex = [NSRegularExpression regularExpressionWithPattern:UUID_PATTERN options:NSRegularExpressionCaseInsensitive error:nil]; int matches = [regex numberOfMatchesInString:self options:0 range:NSMakeRange(0, [self length])]; return (matches == 1); } |
To use this NSString category, you would just do something like this:
NSLog(@"Is %@ a UUID? %@", theTestString, ([theTestString isUUID] ? @"YES" : @"NO")); |
BTW, as far as I am concerned, Google laid a couple of eggs the last two days. Firstly, yesterday (Wednesday, March 13) I tried for almost an hour to get Google I/O tickets, to no avail. I was hoping to attend as my trip to I/O last year was interrupted by my father passing away. Then, this morning, news breaks that Google is killing off Google Reader. My opinion of Google has gone down a couple of notches.
Double Bonus BTW: Happy Pi Day everyone! See you at Stir Trek!!! (Yes, I got tickets for that one. Whew…)
http://stackoverflow.com/questions/20652833/how-to-check-the-validity-of-a-guid-or-uuid-using-nsregularexpression-or-any-o/23564974#23564974