// 需要正则的字符串
NSString *str = @"#呵呵呵#[偷笑] http://test.com/blah_blah";
// 表情的规则
NSString *emotionPattern = @"\\[[0-9a-zA-Z\\u4e00-\\u9fa5]+\\]";
// @的规则
NSString *atPattern = @"@[0-9a-zA-Z\\u4e00-\\u9fa5]+";
// #话题#的规则
NSString *topicPattern = @"#[0-9a-zA-Z\\u4e00-\\u9fa5]+#";
// url链接的规则
NSString *urlPattern = @"\\b(([\\w-]+://?|www[.])[^\\s()<>]+(?:\\([\\w\\d]+\\)|([^[:punct:]\\s]|/)))";
// | 匹配多个条件,相当于or\或
NSString *pattern = [NSString stringWithFormat:@"%@|%@|%@|%@", emotionPattern, atPattern, topicPattern, urlPattern];
// 使用系统的正则类来遍历
NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:pattern options:0 error:nil];
// 2.测试字符串
NSArray *results = [regex matchesInString:str options:0 range:NSMakeRange(0, str.length)];
// 3.遍历结果
for (NSTextCheckingResult *result in results) {
NSLog(@"%@ %@", NSStringFromRange(result.range), [str substringWithRange:result.range]);
}