我想检查数字是否为MGRS。但我无法找到解决方案。目前我正在处理以下代码。
+(BOOL)isValidMGRS:(NSString *)str{
NSString *mgrsRegex = @"(\\d{1,2})([C-X&&[^IO]])([A-Z&&[^IO]])([A-Z&&[^IO]])";
NSPredicate *mgrsTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", mgrsRegex];
return [mgrsTest evaluateWithObject:str];
}
但每次我都变得虚假 还有其他模式来检查MGRS编号吗?
答案 0 :(得分:1)
如果我理解正确且“MGRS编号”为http://en.wikipedia.org/wiki/Military_grid_reference_system,请尝试:
+ (BOOL)isValidMGRS:(NSString *)str{
{
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(\\d{1,2})([C-X&&[^IO]])([A-Z&&[^IO]])([A-Z&&[^IO]])"
options:0
error:&error];
NSRange range = [regex rangeOfFirstMatchInString:str options:0 range:NSMakeRange(0, [str length])];
if (range.location != NSNotFound && error == nil)
{
return YES;
}
return NO;
}