为什么这个正则表达式不匹配空格?
我有以下正则表达式:
([0-9]+),'(.)':([0-9]+),(L|R|'.')
它与此匹配得很好:
1,'a':1,R
但是,如果我将 a 替换为空格,则会失败:
1,' ':1,R
为什么 . 不匹配它?空格不属于字符吗?我无法使用 \s 因为我不想匹配制表符和换行符。我也尝试过:
([0-9]+),'(.| )':([0-9]+),(L|R|'.')
但这也不起作用(而且我没有启用 IgnorePatternWhitespace
)。
I have the following regular expression:
([0-9]+),'(.)':([0-9]+),(L|R|'.')
It matches this just fine:
1,'a':1,R
However, if I replace a with a space, it fails:
1,' ':1,R
Why doesn't . match it? Is a space not classified as a character? I can't use \s because I don't want to match tabs and line breaks. I also tried:
([0-9]+),'(.| )':([0-9]+),(L|R|'.')
But that doesn't work, either (and I don't have IgnorePatternWhitespace
enabled).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我无法重现您所看到的内容:
打印“True”。
引号之间是否有可能还有另一个字符?一些非打印字符?文字从哪里来?
您可以尝试将其更改为:
这样它就可以匹配引号之间的多个字符。
I can't reproduce what you're seeing:
prints "True".
Is it possible that you've got another character between the quotes as well? Some non-printing character? Where is the text coming from?
You could try changing it to:
so it could match more than one character between the quotes.
我没有在 .NET 中尝试过,但点是特定于语言和实现的。尝试:
I haven't tried in .NET, but the dot is language and implementation specific. Try:
使用 \0x0020 将匹配单个空格字符。
Use \0x0020 which will match a single space character.