【发布时间】:2013-11-08 04:06:50
【问题描述】:
我正在尝试检测星号之间的任何单词:
NSString *questionString = @"hello *world*";
NSMutableAttributedString *goodText = [[NSMutableAttributedString alloc] initWithString:questionString]; //should turn the word "world" blue
NSRange range = [questionString rangeOfString:@"\\b\\*(.+?)\\*\\b" options:NSRegularExpressionSearch|NSCaseInsensitiveSearch];
if (range.location != NSNotFound) {
DLog(@"found a word within asterisks - this never happens");
[goodText addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:range];
}
但我从来没有得到积极的结果。正则表达式有什么问题?
【问题讨论】:
-
什么是问题字符串?
-
那里没有单词边界。尝试添加对零个或多个空白字符的搜索。
-
查看此链接对您有帮助。 stackoverflow.com/questions/2753956/…
-
不管空格,但据我所知,仍然没有单词边界。
-
@manujmv 我编辑了问题以显示 questionString。
标签: ios objective-c regex nsattributedstring nsmutableattributedstring