.NET正则表达式错误:[X-Y]范围内以相反的顺序 [英] .NET Regex Error: [x-y] range in reverse order

查看:2689
本文介绍了.NET正则表达式错误:[X-Y]范围内以相反的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个正则表达式,到目前为止,我没有这一点,试了一下,

I am creating a Regex and so far I did this and tried it,

 ^([0][1-9]|1[0-2])[/-.]

和我碰到下面的错误。 。

and I get the following error.

      parsing "^([0][1-9]|1[0-2])[/-.]" - [x-y] range in reverse order. 
      Description: An unhandled exception occurred during the execution of the current web request.          
      Please review the stack trace for more information about the error and where it originated in  
      the code. 

      Exception Details: System.ArgumentException: parsing "^([0][1-9]|1[0-2])[/-.]" - [x-y] range 
      in reverse order.



乱搞了一下,试图找到我的正则表达式的错误后,我只是改变了我的正则表达式

After messing around for a bit and trying to find error in my regex I simply changed me regex to

  ([0][1-9]|1[0-2])[-/.]  so basically only changed this [/-.] to [-/.]

和它的工作。

然后我尝试它的工作其他一些组合。 [/] [/.-] [/? - ]

Then I tried some other combinations which worked.

   [/-]  [/.]  [/.-]  [/-?]

然后我尝试了一些其他人没有工作。

Then I tried some others which didn't work.

   [/-\*]  [/-\+]

因此很明显, - 有一些地方问题,但别人没有问题。 是一个错误吗? ?还是我失去了一些东西

So apparently "-" has problem at some places but no problem at others. Is that a bug? or am I missing something?

谢谢你们为指出 -​​ 被假定为范围,如果它是在中间或不通过\逃脱。

Thanks guys for pointing out that "-" is assumed as range if it is in middle or not escaped by "\".

然而,为什么它[/? - ]工作[/?-\]?或者,是不是真的定义范围在这里还是把它从字面上

However, why did it work for [/-?] or [/-\?], is it really defining range here or taking it literally?

推荐答案

不是一个错误。在字符类(记为 [...] )的 - 字符必须是第一个(一些口味让第一个或最后一个我相信),如果它是被包括作为文字。否则,它预计将表示一个范围,如 0-9 AZ 甚至 /-.

Not a bug. Inside a character class (denoted by […]) the - character must be first (some flavours allow first or last, I believe) if it is to be included as a literal. Otherwise it is expected to denote a range, such as 0-9 or A-Z or even /-..

现在的问题是,根据ASCII(或Unicode),在到来之前的 / ,因此范围被解释为是落后的,等同于指定一个范围 7-4

The problem is that according to ASCII (or Unicode?), the . comes before the /, so the range is interpreted to be backward, equivalent to specifying a range 7-4.

如果您使用 [.- /] ,我不希望一个分析异常,但你不会得到你所期望的结果。

If you used [.-/], I would not expect a parse exception, but you wouldn't get the results you expected.

这篇关于.NET正则表达式错误:[X-Y]范围内以相反的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆