正则表达式最大数字11,必须2位数

时间:2016-10-31 16:31:33

标签: regex

我有一个验证表达式,我试图找出答案。首先,我希望只允许用户输入最多11个而不是11个字符,但允许的数量是可输入的最大数量。我得到了使用下面的代码,并正常工作。

ValidationExpression="^([1-9]|[0-1][0-1])$"

但是,我希望用户也被迫使用2位数。例如,代替1,他们需要输入01.我已尝试过不同的方法,但似乎无法使其发挥作用。

我也试过了,但那也没有用。

ValidationExpression="^([1-9]|[0-1][0-1])${2}"

3 个答案:

答案 0 :(得分:4)

如果您需要在一个步骤中执行此操作(即您无法执行<>检查以及正则表达式),那么应该执行此操作:

ValidationExpression="^(?:0\d|1[01])$"

或者,如果您的语言无法识别\d符号:

ValidationExpression="^(?:0[0-9]|1[01])$"

&#34;匹配(0后跟任何数字)或(1后跟0或1),锚定在输入字符串的开头和结尾。&#34;

答案 1 :(得分:1)

您可以使用此正则表达式


╭─htl@htl-asus  ~/Desktop/
╰─$ git clone git@gitlab.com:user/project.git --branch master --single-branch
Cloning into 'project'...
remote: Counting objects: 657, done.
remote: Compressing objects: 100% (364/364), done.
remote: Total 657 (delta 274), reused 633 (delta 255)
Receiving objects: 100% (657/657), 152.86 KiB | 196.00 KiB/s, done.
Resolving deltas: 100% (274/274), done.
Checking connectivity... done.
╭─htl@htl-asus  ~/Desktop/
╰─$ cd project 
╭─htl@htl-asus  ~/Desktop/project  ‹master› 
╰─$ git checkout -b htl
Switched to a new branch 'htl'
╭─htl@htl-asus  ~/Desktop/project  ‹htl› 
╰─$ git push origin -f
Total 0 (delta 0), reused 0 (delta 0)
remote: 
remote: Create merge request for htl:
remote:   https://gitlab.com/user/project/merge_requests/new?merge_request%5Bsource_branch%5D=htl
remote: 
To git@gitlab.com:user/project.git
 + 9a1e13c...9463f53 htl -> htl (forced update)
╭─htl@htl-asus  ~/Desktop/project  ‹htl› 
╰─$ git checkout -b htl_test_mr
Switched to a new branch 'htl_test_mr'
╭─htl@htl-asus  ~/Desktop/project  ‹htl_test_mr› 
╰─$ touch test
╭─htl@htl-asus  ~/Desktop/project  ‹htl_test_mr*› 
╰─$ git add test 
╭─htl@htl-asus  ~/Desktop/project  ‹htl_test_mr*› 
╰─$ git commit -m 'add test file'
[htl_test_mr 0aa06d1] add test file
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test
╭─htl@htl-asus  ~/Desktop/project  ‹htl_test_mr› 
╰─$ git push origin htl_test_mr
Counting objects: 11, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 270 bytes | 0 bytes/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: 
remote: Create merge request for htl_test_mr:
remote:   https://gitlab.com/user/project/merge_requests/new?merge_request%5Bsource_branch%5D=htl_test_mr
remote: 
To git@gitlab.com:user/project.git
 * [new branch]      htl_test_mr -> htl_test_mr
╭─htl@htl-asus  ~/Desktop/project  ‹htl_test_mr› 
╰─$ git checkout htl
Switched to branch 'htl'
╭─htl@htl-asus  ~/Desktop/project  ‹htl› 
╰─$ git pull
Already up-to-date.
╭─htl@htl-asus  ~/Desktop/project  ‹htl› 
╰─$ git pull
Already up-to-date. ╭─htl@htl-asus ~/Desktop/project ‹htl› ╰─$ git fetch --all Fetching origin ╭─htl@htl-asus ~/Desktop/project ‹htl› ╰─$

这表示输入一个数字/\b(?:[0][\d]|[1][01])\b/ ,然后输入0或输入0-9,然后输入10。它的两边都是字边界,它是一个非捕获组。试一试here

答案 2 :(得分:1)

要匹配从0112的填充的2位数字,您可以使用

ValidationExpression="^(0[1-9]|1[01])$"

请参阅regex demo

表达式匹配:

  • ^
  • ( - 组的开始(此处,捕获组用于提高可读性,也可以使用非捕获组)
    • 0 - 零
    • [1-9] - 19数字
    • | - 或
    • 1 - 1
    • [01] - 01数字
  • ) - 小组结尾
  • $ - 字符串结束。