Objective-C语言检查字符串是否与模式匹配
示例
NSString *testString1 = @"(555) 123-5678";
NSString *testString2 = @"not a phone number";
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^\\(\\d{3}\\) \\d{3}\\-\\d{4}$"
options:NSRegularExpressionCaseInsensitive error:&error];
NSInteger result1 = [regex numberOfMatchesInString:testString1 options:0 range:NSMakeRange(0, testString1.length)];
NSInteger result2 = [regex numberOfMatchesInString:testString2 options:0 range:NSMakeRange(0, testString2.length)];
NSLog(@"Is string 1 a phone number? %@", result1 > 0 ? @"YES" : @"NO");
NSLog(@"Is string 2 a phone number? %@", result2 > 0 ? @"YES" : @"NO");输出将显示第一个字符串是号码,第二个不是。
热门推荐
10 老公生日祝福语简短好看
11 给儿子祝福语简短霸气
12 宝宝100日简短祝福语
13 新年给妈妈简短祝福语
14 鼓励弟弟高考祝福语简短
15 生日祝福语简短发小
16 潮汕话新人祝福语简短
17 新年的好祝福语简短
18 给孙子祝福语简短独特