【发布时间】:2016-06-19 03:29:43
【问题描述】:
pattern = @"(\\$.*?\\$)";
regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];
matches = [regex matchesInString:self options:0 range: searchedRange];
for (NSTextCheckingResult* match in matches) {
[string addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:[match range]];
}
我正在使用上面的代码为两个美元符号之间的文本着色。因此,以下字符串中的 hello world 文本将变为蓝色,并移除美元符号:
@"This won't be blue. $hello world$. This won't be blue";
但是,现在我希望单个美元符号不被涂成蓝色。我只希望模式在第一个美元符号后面是 a-zA-Z0-9 中的一个字符时匹配,同样第二个美元符号前面应该是 a-zA-Z0-9 中的一个字符。我该怎么做?
【问题讨论】:
-
美元之间的内容可以是单个字符吗?
标签: ios objective-c regex