Java 如何使用正则表达式验证范围 1-99?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4012985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 08:23:43  来源:igfitidea点击:

How can I validate the range 1-99 using a regex?

javaregexvalidation

提问by Jimmy

I need to validate some user input, to ensure a number entered is in the range of 1-99 inclusive. These must be whole (Integer) values

我需要验证一些用户输入,以确保输入的数字在 1-99 的范围内。这些必须是整数(整数)值

Preceeding 0 is permitted, but optional

前面的 0 是允许的,但可选

Valid values

有效值

  1. 1
  2. 01
  3. 10
  4. 99
  5. 09
  1. 1
  2. 01
  3. 10
  4. 99
  5. 09

Invalid values

无效值

  1. 0
  2. 007
  3. 100
  4. 10.5
  5. 010
  1. 0
  2. 007
  3. 100
  4. 10.5
  5. 010

So far I have the following regex that I've worked out : ^0?([1-9][0-9])$

到目前为止,我已经制定了以下正则表达式: ^0?([1-9][0-9])$

This allows an optional 0 at the beginning, but isn't 100% correct as 1is not deemed as valid

这允许在开头使用可选的 0,但不是 100% 正确,因为1不被视为有效

Any improvements/suggestions?

任何改进/建议?

采纳答案by developmentalinsanity

Off the top of my head (not validated)

在我的头顶上(未验证)

^(0?[1-9]|[1-9][0-9])$

^(0?[1-9]|[1-9][0-9])$

回答by Alin Purcaru

Here you go:

干得好:

^(\d?[1-9]|[1-9]0)$

Meaning that you allow either of

这意味着您允许

  1. 1 to 9 or 01 to 09, 11 to 19, 21 to 29, ..., 91 to 99
  2. 10, 20, ..., 90
  1. 1 到 9 或 01 到 09、11 到 19、21 到 29、...、91 到 99
  2. 10, 20, ..., 90

回答by red-X

^(([0-9][1-9])|([1-9][0-9])|[1-9])$

should work

应该管用

回答by Gadolin

String d = "11"

if (d.length() <= 2 && d.length() >=1) {
    try {
        Integer i = Integer.valueOf(d);
        return i <= 99 && i >= 0
    }
    catch (NumberFormatException e) {
        return false;
    }
}

回答by Lalith

^[0-9]{1,2}$ 

should work too (it'll will match 00 too, hope it's a valid match).

也应该工作(它也会匹配 00,希望它是一个有效的匹配)。

回答by Mark Thomas

Why is regex a requirement? It is not ideal for numeric range calculations.

为什么需要正则表达式?它不是数字范围计算的理想选择。

Apache commons has IntegerValidator with the following:

Apache commons 有 IntegerValidator 具有以下内容:

isInRange(value, 1, 99)

In addition, if you're using Spring, Struts, Wicket, Hibernate, etc., you already have access to a range validator. Don't reinvent the wheel with Regular Expressions.

此外,如果您正在使用 Spring、Struts、Wicket、Hibernate 等,那么您已经可以访问范围验证器。不要用正则表达式重新发明轮子。

回答by Yunus Usmani

I think it should be like...

我觉得应该是这样...

^(0[1-9]|[1-9][0-9])$

回答by Androbin

This one worked for myself:

这个对我自己有用:

([1-9][0-9])|(0?[1-9])

It checks for 10-99 or 1-9 => 1-99 with one leading zero allowed

它检查 10-99 或 1-9 => 1-99 并允许有一个前导零

回答by Marcio Zabeu

Just do:

做就是了:

^([0]?[1-9]{1,2})$

The range will be set from 0to 9and the {1,2}means min digits = 1 and max digits = 2.

范围将设置为从09{1,2}平均值最小位数 = 1 和最大位数 = 2。

It will accept, for example: 0, 00, 01, 11, 45, 99,etc... It will not accept, for example: 000, 1.2, 5,4, 3490,etc...

它会接受,例如:0, 00, 01, 11, 45, 99,etc... 它不会接受,例如:000, 1.2, 5,4, 3490,etc...

回答by abhisheknirmal

This is the simplest possible, I can think of:

这是最简单的可能,我能想到:

^([1-9][0-9]?)$

^([1-9][0-9]?)$

Allows only 1-99 both inclusive.

仅允许 1-99 包括两者。