Archive for 14th March 2013

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}$"
    NSRegularExpression *regex;
    regex = [NSRegularExpression regularExpressionWithPattern:UUID_PATTERN
    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…)