正则表达式问题
在 .NET 4.0 中,reg ex,
"^ABC(: ([^=]+(?<! )=(?! )[^,]+(?<! )(,(?! )|$))+)?$"
匹配什么?
一些示例会有很大帮助。
我对以下结果感到非常惊讶。 上述表达式匹配“ABC: X=12,Y=1.79769313486232E+308”。但对于“ABC: X=12,Y=1,79769313486232E+308”则失败。唯一的区别是双精度数的小数符号。
谢谢。
In .NET 4.0 What does the reg ex,
"^ABC(: ([^=]+(?<! )=(?! )[^,]+(?<! )(,(?! )|$))+)?$"
matches to?
Some sample examples would be of much help.
I am quite surprised with following results.
The above expression matches, "ABC: X=12,Y=1.79769313486232E+308". But it fails for "ABC: X=12,Y=1,79769313486232E+308". The only difference is the decimal symbol for the double number.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看一下
[^,]
,它基本上表示在 = (=(?! )
) 之后匹配任何没有,
在其中。正则表达式并不是很优雅:
即使像
ABC
这样的东西也会匹配。像ABC: X=1Y=1
这样的东西也会匹配。我想说,不要使用它并根据您的需要组装适当的正则表达式。Look at the
[^,]
that basically says that after the = (=(?! )
) match anything that doesn't have a,
in it.The Regex is not really elegant:
Even something like
ABC
would match. Something likeABC: X=1Y=1
would also match. I would say, don't use this and assemble a proper regex for what you need.你说:
没有任何上下文,我不确定目的是什么 是完全合法的。
匹配上述字符串的格式是,但我可以明白为什么匹配第一个而不是第二个的格式
1.79769313486232E+308
是一个非常大的数字的科学记数法(+308 基本上意味着将小数点向右移动 308 位),它是带点的合法数字,但不带逗号。 。确实,某些语言环境可能使用逗号作为十进制字符而不是点,但科学记数法已标准化为使用点,编程语言和其他计算机应用程序也是如此 将使用这种格式的数字,因此强制其为点并防止使用逗号是合法的。
为了演示为什么这很重要,如果在此示例中允许使用逗号,则会产生歧义,如下所示。到
Y
值结束的位置,因为逗号已用于显示X
值的结尾,使用逗号代替Y 中的点
可以让计算机认为Y
是1
,这是不正确的。You said:
Without any context, I'm not sure what the purpose of matching the above strings is, but I can see why it would be perfectly legitimate to match the first but not the second.
The format of
1.79769313486232E+308
is scientific notation for a very large number (the +308 basically means move the decimal point 308 places to the right). It is a legitimate number with the dot, but not with the comma.It is true that some locales may use a comma as a decimal character rather than a dot, but scientific notation is standardised to use the dot, as are programming languages and other computer applications that would use numbers in this format, so it is legitimate to enforce it to be a dot and prevent the comma from being used.
To demonstrate why this is important, if the comma were to be allowed in this example, it would create an ambiguity as to where the value of
Y
ended, because comma is already being used to show the end of the value ofX
, using a comma instead of the point inY
could make a computer think that the value ofY
is1
, which would be incorrect.