NSRegularExpression 字符串由以下分隔
我有一个字符串,例如 @"You've Earned Commentator and 4 ##other$$徽章"
。我想检索由 ##
和 $$
分隔的子字符串 @"other"
。我做了一个像这样的 NSRegularExpression:
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"##(.*)$$" options:NSRegularExpressionCaseInsensitive error:nil];
这完全忽略了 $$
并返回以 ##
开头的内容。我做错了什么?谢谢。
I have a string for example @"You've earned Commentator and 4 ##other$$ badges"
. I want to retreive the substring @"other"
, which is delimited by ##
and $$
. I made a NSRegularExpression like this:
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"##(.*)$" options:NSRegularExpressionCaseInsensitive error:nil];
This completely ignores $$
and returns stuff starting with ##
. What am I doing wrong? thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那是因为“$”是一个特殊字符,代表行尾。尝试
\$\$
对其进行转义并告诉解析器您想要这些字符。Thats because '$' is a special character that represents the end of the line. Try
\$\$
to escape it and tell the parser you want the characters.在这种情况下我不会使用正则表达式,因为字符串攻击非常简单。不需要编译表达式的开销。
I wouldn't use a regex in this situation, since the string bashing is so simple. No need for the overhead of compiling the expression.