Ron Fleckner
Ron Fleckner

Reputation: 1

RegexKitLite replacement pattern changing case of found match

I'd like to change the case (ie, lowercase to uppercase) of found matches using RegexKitLite but don't know how or if it's possible. In PCRE regex, you can have in the replacement pattern something like \u$1 to uppercase the found match of group 1. I can't see how to do that. Can someone please let me know how?

Thanks in advance

Upvotes: 0

Views: 347

Answers (1)

johne
johne

Reputation: 6770

Use RegexKitLite 4.0s Blocks methods:

NSString *string = @"An example of lowercase to uppercase.";

NSString *replaced = [string stringByReplacingOccurrencesOfRegex:@"\\w+" usingBlock:^NSString *(NSInteger captureCount, NSString * const capturedStrings[captureCount], const NSRange capturedRanges[captureCount], volatile BOOL * const stop) {
  return([capturedStrings[0] capitalizedString]);
}];

NSLog(@"Replaced: '%@'", replaced);

Output when run:

2010-08-22 14:25:20.047 RegexKitLite[33454:a0f] Replaced: 'An Example Of Lowercase To Uppercase.'

Upvotes: 0

Related Questions