regexmacoscocoautilaunch-services

Regex to validate Uniform Type Identifier


Uniform Type Identifiers (UTIs) are the modern method to identify file types on Mac OS X. In Apple's documentation it says:

A UTI is defined as a string (CFString) that follows a reverse Domain Name System (DNS) convention.

However, the UTI-related functions in LaunchServices do not provide any method to validate a UTI, i.e. check whether a given string is a UTI and conforms to the UTI string format (i.e. uses only legal characters, etc.).

The Wikipedia page on UTIs says:

UTIs use a reverse-DNS naming structure. Names may include the ASCII characters A-Z, a-z, 0-9, hyphen ("-"), and period ("."), and all Unicode characters above U+007F.[1] Colons and slashes are prohibited for compatibility with Macintosh and POSIX file path conventions.

What would a regex to validate a UTI look like?


Solution

  • Through some further searching I found this on the Reverse domain name notation Wikipedia page:

    ^[A-Za-z]{2,6}((?!-)\\.[A-Za-z0-9-]{1,63}(?<!-))+$
    

    Concocted the following function to validate an NSString containing a UTI:

    BOOL UTTypeIsValid(NSString *inUTI) {
        NSString *reverseDNSRegEx = @"^[A-Za-z]{2,6}((?!-)\\.[A-Za-z0-9-]{1,63}(?<!-))+$";
        NSPredicate *test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", reverseDNSRegEx];
        return [test evaluateWithObject:inUTI];
    }