我正在寻找一种从用户输入中提取日期的解决方案。它应该支持:
欢迎付费解决方案以及开源(非GPL)。
答案 0 :(得分:2)
Apple在10.7中包括NSDataDetector 除了URL,电话号码等,它还支持日期(NSTextCheckingTypeDate)检测。 (似乎Mail.app广泛使用这些探测器)
此示例检测“string”中的所有日期,并记录匹配(如果有)位置和&长度:
NSError* error = NULL;
NSDataDetector* detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeDate error:&error];
NSArray* matches = [detector matchesInString:string options:0 range:NSMakeRange(0, [string length])];
for (NSTextCheckingResult* match in matches)
{
NSRange matchRange = [match range];
NSLog(@"Match at position:%lu with length:%lu", matchRange.location, matchRange.length);
}