正数表示0到12之间的正数

时间:2012-09-19 15:01:35

标签: c# regex validation

我试过没有运气来搜索互联网上的正则表达式来检查:

  • 积极的#
  • 0到12之间

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

 Regex rxValidHeightInches = new Regex();

非常感谢任何帮助!

由于

2 个答案:

答案 0 :(得分:6)

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

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

答案 1 :(得分: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»

<强> SCREENSHOT

enter image description here


这个包含0 - 12

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

enter image description here