【问题标题】:Regular Expression for a Positive Number between 0 and 120 到 12 之间正数的正则表达式
【发布时间】:2012-09-19 15:01:34
【问题描述】:

我尝试在互联网上搜索一个正则表达式来检查:

  • 积极的#
  • 0 到 12 之间

我需要把它放在这行代码的括号中

 Regex rxValidHeightInches = new Regex();

任何帮助将不胜感激!

谢谢

【问题讨论】:

标签: c# regex validation


【解决方案1】:

这应该可以^(\d|(1[0-2]))$

var rxValidHeightInches = new Regex("^(\\d|(1[0-2]))$");

【讨论】:

  • 假设你的意思是在“包容性”之间
【解决方案2】:

试试这个。

(?<!-)(\b((1[01])|[1-9])\b)

它匹配 1 - 91011。总是排除负数。

说明

-Assert that it is impossible to match the regex below with the match ending at this position (negative lookbehind) «(?<!-)»
---Match the character “-” literally «-»
-Match the regular expression below and capture its match into backreference number 1 «(\b((1[01])|[1-9])\b)»
---Assert position at a word boundary «\b»
---Match the regular expression below and capture its match into backreference number 2 «((1[01])|[1-9])»
------Match either the regular expression below (attempting the next alternative only if this one fails) «(1[01])»
---------Match the regular expression below and capture its match into backreference number 3 «(1[01])»
------------Match the character “1” literally «1»
------------Match a single character present in the list “01” «[01]»
------Or match regular expression number 2 below (the entire group fails if this one fails to match) «[1-9]»
---------Match a single character in the range between “1” and “9” «[1-9]»
---Assert position at a word boundary «\b»

屏幕截图


这一项包括 0 - 12

(?<!-)(\b((1[0-2])|[0-9])\b)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-11
    • 1970-01-01
    相关资源
    最近更新 更多