objective c - search NSArray with regex -
i have array of names. if of name there on inserting new name want append counter eg john (02) if john present in array john (03) if third entry of name john.
is there way filter array regex can filter records pattern "john (xx)"?
yup. have loop through array , check regex. have this, since if check if array contains string, won't return true if search "john" , 1 in array "john1"
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]; (int = 0; < [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 2 currentvalue = 2; } else { currentvalue = [[split objectatindex:1] intvalue]; currentvalue++; } nsstring *newstring = [nsstring stringwithformat:@"%@,%d", nametoadd, currentvalue]; [testarray replaceobjectatindex:i withobject:newstring]; } } (nsstring *string in testarray) { nslog(@"%@", string); }
this replace "john" "john,2", , if search "john" third time replace "john,3".
hope helps
Comments
Post a Comment