简体   繁体   English

如何强制字符串中的最后一个字符在一定范围内?

[英]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.

相关问题 匹配某个范围内的单个正则表达式字符 - Match a single Regex character that is within a certain range 如何使用perl正则表达式删除某个可识别字符串中的字符? - How to remove a character within a certain identifiable string using perl regex? R:查找数字是否在字符串中的范围内 - R: find if number is within range in a character string 在Oracle中,如何选择包含特定数值范围内的字符的行? - In Oracle, how do I select rows which contain a character within a certain numeric range? Bash从字符串中剪切第一个和/或最后一个字符,但前提是它是某个特定字符 - Bash shave a first and/or last character from string, but only if it is a certain character 在动态字符串中,如何在特定字符处开始子字符串并在特定字符处结束子字符串? - In a dynamic string, how to start a substring at a certain character and end it at certain character? 如果在最后一次出现特定字符后包含某些字符串,则正则表达式匹配 - Regex match if certain string is contained after last occurrence of specific character 如何用最后一个匹配的字符和常量替换字符串中最后一个匹配的字符 - how to replace the last matched character in string with last matched character and constant 如何计算字符串中某些字符的频率? - How to count frequencies of certain character in a string? 如何检查字符串中的最后一个字符是否为“空格”? - How to check if last character in a string is 'space'?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM