Iphone 匹配字符串格式

Iphone 匹配字符串格式,iphone,objective-c,ios,Iphone,Objective C,Ios,如何执行字符串匹配,就像regex 例如,我有一个字符串数组,我想匹配这些字符串的格式是否为“abc def xxx”,其中xxx是类似1,11,12,100,111,等等的数字 如何实现这一点?有一个NSRegularExpression类,请看一看 有一个NSRegularExpression类,请看一看 阵列: NSArray *array1 = [NSArray arrayWithObjects:@"abc def 1", @"abc def 64", @"abc def 853", @

如何执行字符串匹配,就像
regex

例如,我有一个字符串数组,我想匹配这些字符串的格式是否为
“abc def xxx”
,其中
xxx
是类似
1
11
12
100
111
,等等的数字


如何实现这一点?

有一个NSRegularExpression类,请看一看


有一个NSRegularExpression类,请看一看


阵列:

NSArray *array1 = [NSArray arrayWithObjects:@"abc def 1", @"abc def 64", @"abc def 853", @"abc def 14", nil];
NSArray *array2 = [NSArray arrayWithObjects:@"abc def 3", @"abc def 856", @"abc def 36", @"abc def 5367", nil];
NSString *regex = @"abc def [0-9]{1,3}";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ALL description MATCHES %@", regex];
BOOL allStringsMatch = [predicate evaluateWithObject:array1]; 
// OUTPUT: YES
allStringsMatch = [predicate evaluateWithObject:array2];
// OUTPUT: NO
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"description MATCHES %@", regex];
NSArray *unmatchedStrings = [array2 filteredArrayUsingPredicate:predicate];
// OUTPUT: { @"abc def 3", @"abc def 856", @"abc def 36" }
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT description MATCHES %@", regex];
NSArray *unmatchedStrings = [array2 filteredArrayUsingPredicate:predicate];
// OUTPUT: { @"abc def 5367" }
正则表达式:

NSArray *array1 = [NSArray arrayWithObjects:@"abc def 1", @"abc def 64", @"abc def 853", @"abc def 14", nil];
NSArray *array2 = [NSArray arrayWithObjects:@"abc def 3", @"abc def 856", @"abc def 36", @"abc def 5367", nil];
NSString *regex = @"abc def [0-9]{1,3}";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ALL description MATCHES %@", regex];
BOOL allStringsMatch = [predicate evaluateWithObject:array1]; 
// OUTPUT: YES
allStringsMatch = [predicate evaluateWithObject:array2];
// OUTPUT: NO
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"description MATCHES %@", regex];
NSArray *unmatchedStrings = [array2 filteredArrayUsingPredicate:predicate];
// OUTPUT: { @"abc def 3", @"abc def 856", @"abc def 36" }
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT description MATCHES %@", regex];
NSArray *unmatchedStrings = [array2 filteredArrayUsingPredicate:predicate];
// OUTPUT: { @"abc def 5367" }
检查数组中的所有字符串是否与正则表达式匹配:

NSArray *array1 = [NSArray arrayWithObjects:@"abc def 1", @"abc def 64", @"abc def 853", @"abc def 14", nil];
NSArray *array2 = [NSArray arrayWithObjects:@"abc def 3", @"abc def 856", @"abc def 36", @"abc def 5367", nil];
NSString *regex = @"abc def [0-9]{1,3}";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ALL description MATCHES %@", regex];
BOOL allStringsMatch = [predicate evaluateWithObject:array1]; 
// OUTPUT: YES
allStringsMatch = [predicate evaluateWithObject:array2];
// OUTPUT: NO
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"description MATCHES %@", regex];
NSArray *unmatchedStrings = [array2 filteredArrayUsingPredicate:predicate];
// OUTPUT: { @"abc def 3", @"abc def 856", @"abc def 36" }
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT description MATCHES %@", regex];
NSArray *unmatchedStrings = [array2 filteredArrayUsingPredicate:predicate];
// OUTPUT: { @"abc def 5367" }
array1中的所有对象都与正则表达式匹配,但array2包含与正则表达式不匹配的字符串@“abc def 5367”

获取匹配字符串:

NSArray *array1 = [NSArray arrayWithObjects:@"abc def 1", @"abc def 64", @"abc def 853", @"abc def 14", nil];
NSArray *array2 = [NSArray arrayWithObjects:@"abc def 3", @"abc def 856", @"abc def 36", @"abc def 5367", nil];
NSString *regex = @"abc def [0-9]{1,3}";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ALL description MATCHES %@", regex];
BOOL allStringsMatch = [predicate evaluateWithObject:array1]; 
// OUTPUT: YES
allStringsMatch = [predicate evaluateWithObject:array2];
// OUTPUT: NO
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"description MATCHES %@", regex];
NSArray *unmatchedStrings = [array2 filteredArrayUsingPredicate:predicate];
// OUTPUT: { @"abc def 3", @"abc def 856", @"abc def 36" }
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT description MATCHES %@", regex];
NSArray *unmatchedStrings = [array2 filteredArrayUsingPredicate:predicate];
// OUTPUT: { @"abc def 5367" }
获取不匹配的字符串:

NSArray *array1 = [NSArray arrayWithObjects:@"abc def 1", @"abc def 64", @"abc def 853", @"abc def 14", nil];
NSArray *array2 = [NSArray arrayWithObjects:@"abc def 3", @"abc def 856", @"abc def 36", @"abc def 5367", nil];
NSString *regex = @"abc def [0-9]{1,3}";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ALL description MATCHES %@", regex];
BOOL allStringsMatch = [predicate evaluateWithObject:array1]; 
// OUTPUT: YES
allStringsMatch = [predicate evaluateWithObject:array2];
// OUTPUT: NO
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"description MATCHES %@", regex];
NSArray *unmatchedStrings = [array2 filteredArrayUsingPredicate:predicate];
// OUTPUT: { @"abc def 3", @"abc def 856", @"abc def 36" }
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT description MATCHES %@", regex];
NSArray *unmatchedStrings = [array2 filteredArrayUsingPredicate:predicate];
// OUTPUT: { @"abc def 5367" }

注意,这里谓词中的“description”是NSString的
-description
方法。

数组:

NSArray *array1 = [NSArray arrayWithObjects:@"abc def 1", @"abc def 64", @"abc def 853", @"abc def 14", nil];
NSArray *array2 = [NSArray arrayWithObjects:@"abc def 3", @"abc def 856", @"abc def 36", @"abc def 5367", nil];
NSString *regex = @"abc def [0-9]{1,3}";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ALL description MATCHES %@", regex];
BOOL allStringsMatch = [predicate evaluateWithObject:array1]; 
// OUTPUT: YES
allStringsMatch = [predicate evaluateWithObject:array2];
// OUTPUT: NO
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"description MATCHES %@", regex];
NSArray *unmatchedStrings = [array2 filteredArrayUsingPredicate:predicate];
// OUTPUT: { @"abc def 3", @"abc def 856", @"abc def 36" }
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT description MATCHES %@", regex];
NSArray *unmatchedStrings = [array2 filteredArrayUsingPredicate:predicate];
// OUTPUT: { @"abc def 5367" }
正则表达式:

NSArray *array1 = [NSArray arrayWithObjects:@"abc def 1", @"abc def 64", @"abc def 853", @"abc def 14", nil];
NSArray *array2 = [NSArray arrayWithObjects:@"abc def 3", @"abc def 856", @"abc def 36", @"abc def 5367", nil];
NSString *regex = @"abc def [0-9]{1,3}";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ALL description MATCHES %@", regex];
BOOL allStringsMatch = [predicate evaluateWithObject:array1]; 
// OUTPUT: YES
allStringsMatch = [predicate evaluateWithObject:array2];
// OUTPUT: NO
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"description MATCHES %@", regex];
NSArray *unmatchedStrings = [array2 filteredArrayUsingPredicate:predicate];
// OUTPUT: { @"abc def 3", @"abc def 856", @"abc def 36" }
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT description MATCHES %@", regex];
NSArray *unmatchedStrings = [array2 filteredArrayUsingPredicate:predicate];
// OUTPUT: { @"abc def 5367" }
检查数组中的所有字符串是否与正则表达式匹配:

NSArray *array1 = [NSArray arrayWithObjects:@"abc def 1", @"abc def 64", @"abc def 853", @"abc def 14", nil];
NSArray *array2 = [NSArray arrayWithObjects:@"abc def 3", @"abc def 856", @"abc def 36", @"abc def 5367", nil];
NSString *regex = @"abc def [0-9]{1,3}";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ALL description MATCHES %@", regex];
BOOL allStringsMatch = [predicate evaluateWithObject:array1]; 
// OUTPUT: YES
allStringsMatch = [predicate evaluateWithObject:array2];
// OUTPUT: NO
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"description MATCHES %@", regex];
NSArray *unmatchedStrings = [array2 filteredArrayUsingPredicate:predicate];
// OUTPUT: { @"abc def 3", @"abc def 856", @"abc def 36" }
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT description MATCHES %@", regex];
NSArray *unmatchedStrings = [array2 filteredArrayUsingPredicate:predicate];
// OUTPUT: { @"abc def 5367" }
array1中的所有对象都与正则表达式匹配,但array2包含与正则表达式不匹配的字符串@“abc def 5367”

获取匹配字符串:

NSArray *array1 = [NSArray arrayWithObjects:@"abc def 1", @"abc def 64", @"abc def 853", @"abc def 14", nil];
NSArray *array2 = [NSArray arrayWithObjects:@"abc def 3", @"abc def 856", @"abc def 36", @"abc def 5367", nil];
NSString *regex = @"abc def [0-9]{1,3}";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ALL description MATCHES %@", regex];
BOOL allStringsMatch = [predicate evaluateWithObject:array1]; 
// OUTPUT: YES
allStringsMatch = [predicate evaluateWithObject:array2];
// OUTPUT: NO
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"description MATCHES %@", regex];
NSArray *unmatchedStrings = [array2 filteredArrayUsingPredicate:predicate];
// OUTPUT: { @"abc def 3", @"abc def 856", @"abc def 36" }
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT description MATCHES %@", regex];
NSArray *unmatchedStrings = [array2 filteredArrayUsingPredicate:predicate];
// OUTPUT: { @"abc def 5367" }
获取不匹配的字符串:

NSArray *array1 = [NSArray arrayWithObjects:@"abc def 1", @"abc def 64", @"abc def 853", @"abc def 14", nil];
NSArray *array2 = [NSArray arrayWithObjects:@"abc def 3", @"abc def 856", @"abc def 36", @"abc def 5367", nil];
NSString *regex = @"abc def [0-9]{1,3}";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ALL description MATCHES %@", regex];
BOOL allStringsMatch = [predicate evaluateWithObject:array1]; 
// OUTPUT: YES
allStringsMatch = [predicate evaluateWithObject:array2];
// OUTPUT: NO
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"description MATCHES %@", regex];
NSArray *unmatchedStrings = [array2 filteredArrayUsingPredicate:predicate];
// OUTPUT: { @"abc def 3", @"abc def 856", @"abc def 36" }
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT description MATCHES %@", regex];
NSArray *unmatchedStrings = [array2 filteredArrayUsingPredicate:predicate];
// OUTPUT: { @"abc def 5367" }

注意这里谓词中的“description”是NSString的
-description
方法。

非常感谢!谢谢!:)非常感谢你!谢谢!:)