Replacing numbers in an NSString with objects from NSArray

In UITextView, users can enter numbers formatted as {{1}} or {{177}} . In the preview field, like here, on SO, I would like to replace this template with the value from NSArray, where the number corresponds to the line in the array.

I have two ideas to solve this problem:

  • Count all occurrences with NSRegularExpression, view and replace them.
  • Create an array of words from NSString and go through the array, replacing the entries and return the NSString together.

Which of these two options is more efficient and reliable? Is there another way?

- update - This is a regular expression to search for a pattern: @"\\{\\{([1-9]|[1-9][0-9]|[1-9][0-9][0-9])\\}\\}" . How can I use this in an integer loop to replace a pattern?

0
source share
3 answers

Here are the answers using NSRegularExpression :

 NSString * input = @"{{83}} fjsdkfjds {{122}}"; NSMutableString * strToMakeReplacements = [[NSMutableString alloc] initWithString:input]; NSError * error = nil; NSRegularExpression * regex = [NSRegularExpression regularExpressionWithPattern:@"\\{\\{([1-9]|[1-9][0-9]|[1-9][0-9][0-9])\\}\\}" options:NSRegularExpressionCaseInsensitive error:&error]; NSArray * matches = [regex matchesInString:input options:NSMatchingReportProgress range:NSMakeRange(0, [input length])]; for (int i = [matches count]-1; i>=0 ; i--) { NSTextCheckingResult * match = [matches objectAtIndex:i]; NSRange matchRange = match.range; NSString * numString = [input substringWithRange:NSMakeRange(matchRange.location+2, matchRange.length-4)]; NSInteger num = [numString intValue]; NSString * replacementValue = [self replacementValueForNum:num]; // write this function yourself [strToMakeReplacements replaceCharactersInRange:match.range withString:replacementValue]; } 

Note that you need to avoid the slash in your regular expression so that it becomes \\{\\{([1-9]|[1-9][0-9]|[1-9][0-9][0-9])\\}\\}

+2
source

I would use the second approach, as it seems more convenient than using regular expressions (although I admittedly am not very experienced with them). A.

Given the input string NSString* input , I would filter out non-numeric characters as follows:

 NSCharacterSet* notNumbersSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet]; NSArray * wholeNumbers = [input componentsSeparatedByCharactersInSet:notNumbersSet]; 

Now loop through an array of integers:

 for( NSString* numString in wholeNumbers ) { NSInteger num = [numString intValue]; // ... } 
+1
source
An approach

A NSRegularExpression would be something like this:

 NSRegularExpression * expression = [NSRegularExpression regularExpressionWithPattern:@"\\{\\{(\\d+?)\\}\\}" options:0 error:nil]; NSMutableString * resultString = [aString mutableCopy]; NSTextCheckingResult * result; NSUInteger location = 0; /* Get the first match in the remaining string to search */ while ((result = [expression firstMatchInString:resultString options:0 range:NSMakeRange(location, [resultString length] - location)])) { /* Get the index of the array using the capture group 1 ie (\\d+?) */ NSUInteger index = [[resultString substringWithRange:[result rangeAtIndex:1]] integerValue]; /* Get the string that will replace the match */ NSString * replacementString = [replacementStrings objectAtIndex:index]; /* Replace the string */ [resultString replaceCharactersInRange:result.range withString:replacementString]; /* Skip to the end of the replaced string */ location = result.range.location + [replacementString length]; } 

Since our replacement strings come from an array, I doubt that we have one solution for this solution. Here we find a match, replace it accordingly and then look for the rest of the string to match. We reiterate that there are no more results.

+1
source

All Articles