[英]How to force the last character in a string to be within a certain range?
I have a regular expression which allows the value range '1111' - '999999', excluding any instances of '0': 我有一个允许值范围为“ 1111”-“ 999999”的正则表达式,不包括“ 0”的任何实例:
^([1-9]){4,6}$
This is working for me, except my requirement has been modified. 这对我有用,但我的要求已被修改。 I now have to disallow the last '9' in the range, so that the maximum value of the last character is '8', eg
我现在必须禁止范围中的最后一个'9',以便最后一个字符的最大值为'8',例如
1111 - 9998, or 1111 - 99998, or 1111 - 999998
How can I modify this regular expression to meet the new criteria? 如何修改此正则表达式以满足新条件?
这个怎么样
^([1-9]){3,5}[1-8]$
First option: 第一种选择:
^[1-9]{3,5}[1-8]$
Second option: 第二种选择:
^(?!.*9$)[1-9]{4,6}$
Negative lookahead (?!.*9$)
does not allow 9
to be at the end. 负向超前
(?!.*9$)
不允许以9
结尾。
Third option: 第三种选择:
^(?!.*0)(?!.*9$)\d{4,6}$
Negative lookahead (?!.*0)
does not allow 0
to be in text at all. 负向超前
(?!.*0)
根本不允许0
出现在文本中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.