3

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

  • 一个积极的#
  • 0 到 12 之间

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

 Regex rxValidHeightInches = new Regex();

任何帮助将不胜感激!

谢谢

4

2 回答 2

6

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

var rxValidHeightInches = new Regex("^(\\d|(1[0-2]))$");
于 2012-09-19T15:05:01.720 回答
2

试试这个。

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

它匹配1 - 9or1011。负数总是被排除在外。

解释

-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)

在此处输入图像描述

于 2012-09-19T15:04:19.450 回答