使用正则表达式搜索 NSArray
search NSArray with regex
我有一组名字。如果任何名称已经存在,那么在插入新名称时我想附加计数器,例如 John (02) 如果 John 已经存在于数组中,那么 John (03) 如果它是名称 John 的第三个条目。
有什么方法可以使用正则表达式过滤数组,以便我可以使用模式 "John (xx)" 过滤所有记录?
是的。您必须遍历数组并使用正则表达式进行检查。你必须这样做,因为如果你只是检查数组是否包含你的字符串,如果你搜索 "John" 并且你的数组中唯一的一个是 "John1" 它就不会 return true
NSMutableArray *testArray = [[NSMutableArray alloc] initWithObjects:@"John", @"Steve", @"Alan", @"Brad", nil];
NSString *nameToAdd = @"John";
NSString *regex = [NSString stringWithFormat:@"%@[,]*[0-9]*", nameToAdd];
NSPredicate *myTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
for (int i = 0; i < [testArray count]; i++)
{
NSString *string = [testArray objectAtIndex:i];
if ([myTest evaluateWithObject:string])
{
// Matches
NSLog(@" match !");
int currentValue;
NSArray *split = [string componentsSeparatedByString:@","];
if ([split count] == 1)
{
// Set to 2
currentValue = 2;
}
else
{
currentValue = [[split objectAtIndex:1] intValue];
currentValue++;
}
NSString *newString = [NSString stringWithFormat:@"%@,%d", nameToAdd, currentValue];
[testArray replaceObjectAtIndex:i withObject:newString];
}
}
for (NSString *string in testArray)
{
NSLog(@"%@", string);
}
这会将 "John" 替换为 "John,2",如果您第三次搜索 "John",它将替换为 "John,3"。
希望对您有所帮助
您可以为正则表达式创建一个谓词,然后使用该谓词过滤数组。根据匹配的计数,您可以根据需要更新正在添加的新值。
NSMutableArray *currentNames = ... // the current list of names
NSString *newName = ... // the new name to add
NSString *regex = [NSString stringWithFormat:@"%@ \([0-9]*\)", newName];
NSPredicate *filter = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
NSArray *matches = [currentNames filteredArrayUsingPredicate:filter];
if (matches.count) {
NSString *updatedName = [NSString stringWithFormat:@"%@ (%02d)", newName, matches.count];
[currentNames addObject:updatedName];
} else {
[currentNames addObject:newName];
}
您可以使用 NSPredicate
对数组进行归档。不太熟悉正则表达式,但以下内容似乎还可以:
NSArray *array = @[@"John (01)", @"John (02)", @"John XX"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES 'John [(]\\d{2}[)]'"];
NSArray *result = [array filteredArrayUsingPredicate:predicate];
NSLog( @"%@", result ); // Output John (01), John (02)
我有一组名字。如果任何名称已经存在,那么在插入新名称时我想附加计数器,例如 John (02) 如果 John 已经存在于数组中,那么 John (03) 如果它是名称 John 的第三个条目。
有什么方法可以使用正则表达式过滤数组,以便我可以使用模式 "John (xx)" 过滤所有记录?
是的。您必须遍历数组并使用正则表达式进行检查。你必须这样做,因为如果你只是检查数组是否包含你的字符串,如果你搜索 "John" 并且你的数组中唯一的一个是 "John1" 它就不会 return true
NSMutableArray *testArray = [[NSMutableArray alloc] initWithObjects:@"John", @"Steve", @"Alan", @"Brad", nil];
NSString *nameToAdd = @"John";
NSString *regex = [NSString stringWithFormat:@"%@[,]*[0-9]*", nameToAdd];
NSPredicate *myTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
for (int i = 0; i < [testArray count]; i++)
{
NSString *string = [testArray objectAtIndex:i];
if ([myTest evaluateWithObject:string])
{
// Matches
NSLog(@" match !");
int currentValue;
NSArray *split = [string componentsSeparatedByString:@","];
if ([split count] == 1)
{
// Set to 2
currentValue = 2;
}
else
{
currentValue = [[split objectAtIndex:1] intValue];
currentValue++;
}
NSString *newString = [NSString stringWithFormat:@"%@,%d", nameToAdd, currentValue];
[testArray replaceObjectAtIndex:i withObject:newString];
}
}
for (NSString *string in testArray)
{
NSLog(@"%@", string);
}
这会将 "John" 替换为 "John,2",如果您第三次搜索 "John",它将替换为 "John,3"。
希望对您有所帮助
您可以为正则表达式创建一个谓词,然后使用该谓词过滤数组。根据匹配的计数,您可以根据需要更新正在添加的新值。
NSMutableArray *currentNames = ... // the current list of names
NSString *newName = ... // the new name to add
NSString *regex = [NSString stringWithFormat:@"%@ \([0-9]*\)", newName];
NSPredicate *filter = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
NSArray *matches = [currentNames filteredArrayUsingPredicate:filter];
if (matches.count) {
NSString *updatedName = [NSString stringWithFormat:@"%@ (%02d)", newName, matches.count];
[currentNames addObject:updatedName];
} else {
[currentNames addObject:newName];
}
您可以使用 NSPredicate
对数组进行归档。不太熟悉正则表达式,但以下内容似乎还可以:
NSArray *array = @[@"John (01)", @"John (02)", @"John XX"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES 'John [(]\\d{2}[)]'"];
NSArray *result = [array filteredArrayUsingPredicate:predicate];
NSLog( @"%@", result ); // Output John (01), John (02)