ios之正则表达式
1.RegexKitLite
iOS emoji会使用RegexKitLite stringByReplacingOccurrencesOfRegex进行转换。
[e]表情Unified 编码[/e]
NSString *message = @"😄1111";//self.textField.text;
NSLog(@"%@", message);
NSString *regex = @"\u00A9|\u00AE|[\U0001F004-\U0001F19A]|[\U0001F201-\U0001F6C5]|[\u203C-\u21AA]|[\u231A-\u2B55]|[\u3030-\u303D]|[\u3297-\u3299]|[\u3297-\u3299]|\uE50A";
NSString *str = [message stringByReplacingOccurrencesOfRegex:regex usingBlock:^NSString *(NSInteger captureCount, NSString *const __unsafe_unretained *capturedStrings, const NSRange *capturedRanges, volatile BOOL *const stop) {
NSString *str = capturedStrings[0];
const char *emoji = [str cStringUsingEncoding:NSUTF32StringEncoding];
NSUInteger code = *((NSUInteger *)emoji);
NSString *valueString = [NSString stringWithFormat:@"[e]%x[/e]",code];
return valueString;
}];
NSLog(@"%@",str);
NSString *str = @"[e]1f604[/e]1111";
NSString *regex = @"\\[e\\](.*?)\\[/e\\]";
str = [str stringByReplacingOccurrencesOfRegex:regex usingBlock:^NSString *(NSInteger captureCount, NSString *const __unsafe_unretained *capturedStrings, const NSRange *capturedRanges, volatile BOOL *const stop) {
NSString *str = capturedStrings[1];
int code = 0;
NSScanner *scanner = [NSScanner scannerWithString:str];
[scanner scanHexInt:&code];
//NSString *valueString = [NSString stringWithFormat:@"[e]%x[/e]",code];
NSString *valueString = [Emoji emojiWithCode:code];
return valueString;
}];
NSLog(@"%@",str);
Who Should Read This Document
This document is intended for readers who would like to be able to use regular expressions in their Objective-C applications, whether those applications are for the Mac OS X desktop, or for the iPhone.
This document, and RegexKitLite, is also intended for anyone who has the need to search and manipulate NSString objects. If you've ever used the NSScanner, NSCharacterSet, and NSPredicate classes, or any of the the NSString rangeOf… methods, RegexKitLite is for you.
Regular expressions are a powerful way to search, match and extract, and manipulate strings. RegexKitLite can perform many of the same operations that NSScanner, NSCharacterSet, and NSPredicate perform, and usually do it with far fewer lines of code. As an example, RegexKitLite Cookbook - Parsing CSV Data contains an example that is just over a dozen lines, but is a full featured CSV, or Comma Separated Value, parser that takes a CSV input and turns it in to a NSArray of NSArrays.

浙公网安备 33010602011771号