正则表达式检测 $时出现问题
我正在尝试检测以下表达式:
例如
$john
$或
$mike
我的正则表达式有什么问题?
//Check for $symbol
NSRegularExpression *symbolRegex = [[NSRegularExpression alloc] initWithPattern:@"($[a-zA-Z0-9_]+)"
options:NSRegularExpressionCaseInsensitive
error:nil];
matches = [symbolRegex matchesInString:labelText options:0 range:NSMakeRange(0, [labelText length])];
for (NSTextCheckingResult *result in matches) {
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"symbol://%@",[labelText substringWithRange:result.range]]];
[bodyLabel addCustomLink:url inRange:[result range]];
}
[symbolRegex release];
I am trying to detect the following expression: $
for example
$john
or
$mike
What is wrong with my regex?
//Check for $symbol
NSRegularExpression *symbolRegex = [[NSRegularExpression alloc] initWithPattern:@"($[a-zA-Z0-9_]+)"
options:NSRegularExpressionCaseInsensitive
error:nil];
matches = [symbolRegex matchesInString:labelText options:0 range:NSMakeRange(0, [labelText length])];
for (NSTextCheckingResult *result in matches) {
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"symbol://%@",[labelText substringWithRange:result.range]]];
[bodyLabel addCustomLink:url inRange:[result range]];
}
[symbolRegex release];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来你需要转义 $。
由于它是一个特殊/保留字符,因此需要对其进行转义。
http://www.regular-expressions.info/reference.html
It looks like you need to escape the $.
Since it's a special/reserved character, it needs to be escaped.
http://www.regular-expressions.info/reference.html