Ram*_*mon 8 twitter objective-c nsstring uitextview ios
我正在构建一个Twitter iPhone应用程序,它需要检测何时在UITextView中的字符串中输入主题标签或@ -mention.
如何在NSString中找到前面带有"@"或"#"字符的所有单词?
谢谢你的帮助!
Vad*_*gin 22
您可以使用NSRegularExpression类,其格式为#\ w +(\ w代表单词字符).
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"#(\\w+)" options:0 error:&error];
NSArray *matches = [regex matchesInString:string options:0 range:NSMakeRange(0, string.length)];
for (NSTextCheckingResult *match in matches) {
NSRange wordRange = [match rangeAtIndex:1];
NSString* word = [string substringWithRange:wordRange];
NSLog(@"Found tag %@", word);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6666 次 |
| 最近记录: |