简体   繁体   English

正则表达式问题

[英]Regular expression question

In .NET 4.0 What does the reg ex, 在.NET 4.0中,正则表达式是什么,

"^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". 上面的表达式匹配“ ABC:X = 12,Y = 1.79769313486232E + 308”。 But it fails for "ABC: X=12,Y=1,79769313486232E+308". 但是对于“ ABC:X = 12,Y = 1,79769313486232E + 308”失败。 The only difference is the decimal symbol for the double number. 唯一的区别是双精度数字的十进制符号。

Thanks. 谢谢。

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. 甚至像ABC东西也会匹配。 Something like ABC: X=1Y=1 would also match. ABC: X=1Y=1这样的东西ABC: X=1Y=1也可以匹配。 I would say, don't use this and assemble a proper regex for what you need. 我会说,不要使用它并为您需要的内容组装适当的正则表达式。

You said: 你说:

The above expression matches, ABC: X=12,Y=1.79769313486232E+308". But it fails for "ABC: X=12,Y=1,79769313486232E+308" 上面的表达式匹配,ABC:X = 12,Y = 1.79769313486232E + 308”,但对于“ ABC:X = 12,Y = 1,79769313486232E + 308”却失败

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). 1.79769313486232E+308的格式是非常大的科学记数法(+308本质上是指将小数点向右移动308位)。 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 of X , using a comma instead of the point in Y could make a computer think that the value of Y is 1 , which would be incorrect. 为了说明为什么这很重要,如果在此示例中允许使用逗号,则会对Y的值在何处结束产生歧义,因为逗号已被用于显示X的值的末尾,使用a用逗号代替Y中的点会使计算机认为Y值为1 ,这是不正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM