正则表达式检查两个整数值?

时间:2021-06-15 21:29:31

Is there a regular expression check for a number that is within the range of (for example) 18 and 65?

是否有正则表达式检查(例如)18和65范围内的数字?

[RegularExpression(@"^[18-65]$", ErrorMessage = "Customers must be 
 between the ages of 18 and 65. This is due to our insurance policy.")]

2 个解决方案

#1


1  

^(?:1[89]|[2-5]\d|6[0-5])$

Try this.See demo.

试试这个。看看演示。

https://regex101.com/r/sJ9gM7/121

https://regex101.com/r/sJ9gM7/121

#2


1  

Is there a regular expression check for a number that is within the range of (for example) 18 and 65?

是否有正则表达式检查(例如)18和65范围内的数字?

Yes, there is. @vks already posted a suggestion. But the question is "should I use a regex for that?".

就在这里。 @vks已经发布了一个建议。但问题是“我应该使用正则表达式吗?”。

Regex is not used for matching things like you're trying to. Depends on the language you're using, there are much better alternatives.

正则表达式不用于匹配您正在尝试的内容。取决于您使用的语言,有更好的选择。

[18-65] is invalid Regex, it tries to match 1, 5 or numbers in range [8,6], since the range is illegal, it's not a valid.

[18-65]是无效的正则表达式,它尝试匹配范围[8,6]中的1,5或数字,因为范围是非法的,它不是有效的。

#1


1  

^(?:1[89]|[2-5]\d|6[0-5])$

Try this.See demo.

试试这个。看看演示。

https://regex101.com/r/sJ9gM7/121

https://regex101.com/r/sJ9gM7/121

#2


1  

Is there a regular expression check for a number that is within the range of (for example) 18 and 65?

是否有正则表达式检查(例如)18和65范围内的数字?

Yes, there is. @vks already posted a suggestion. But the question is "should I use a regex for that?".

就在这里。 @vks已经发布了一个建议。但问题是“我应该使用正则表达式吗?”。

Regex is not used for matching things like you're trying to. Depends on the language you're using, there are much better alternatives.

正则表达式不用于匹配您正在尝试的内容。取决于您使用的语言,有更好的选择。

[18-65] is invalid Regex, it tries to match 1, 5 or numbers in range [8,6], since the range is illegal, it's not a valid.

[18-65]是无效的正则表达式,它尝试匹配范围[8,6]中的1,5或数字,因为范围是非法的,它不是有效的。