用于验证统一类型标识符的正则表达式

Regex to validate Uniform Type Identifier

统一类型标识符 (UTI) 是在 Mac OS X 上识别文件类型的现代方法。在 Apple 的 documentation 中它说:

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

但是LaunchServices中的UTI相关函数没有提供任何方法来验证UTI,即检查给定的字符串是否为UTI并符合UTI字符串格式(即仅使用合法字符等) .

关于 UTI 的维基百科页面说:

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.

验证 UTI 的正则表达式是什么样的?

通过进一步搜索,我在 Reverse domain name notation 维基百科页面上找到了这个:

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

编写了以下函数来验证包含 UTI 的 NSString:

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];
}