开发者

RegexKitLite replacement pattern changing case of found match

开发者 https://www.devze.com 2023-01-12 03:25 出处:网络
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 pa

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


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.'
0

精彩评论

暂无评论...
验证码 换一张
取 消