[英]Allow any digit in a string other than 9 and 16 digits
^(?![\\s\\S]*(\\d{16})|[\\s\\S]*(\\d{9}))[\\s\\S]*
The above regex does not allow a number greater than 10 digits in the string. 上述正则表达式不允许字符串中的数字大于10位。 Example, if user enters test 1234567891. The text is a valid text.
例如,如果用户输入测试1234567891.该文本是有效文本。 We should allow user to enter this text.
我们应该允许用户输入此文本。 The user should only not enter a 9 digit number or a 16 digit number.
用户不应输入9位数字或16位数字。 Example, test 123456789 should be invalid.
例如,测试123456789应无效。 How to modify the regex.
如何修改正则表达式。
Is this requirement best served by a regexp ? 正则表达式最能满足此要求吗? I think it would be much more readable to check the string length, and if you have a number.
我认为这将是更具可读性检查字符串的长度,如果你有一个数字。
Some people, when confronted with a problem, think "I know, I'll use regular expressions."
有些人在遇到问题时会认为“我知道,我会使用正则表达式”。 Now they have two problems.
现在他们有两个问题。
Don't use a regex for this kind of check. 不要使用正则表达式进行此类检查。 Java has
.length()
on strings: Java在字符串上有
.length()
:
private static final Pattern DIGITS = "\\d+";
public boolean inputOK(String input)
{
Matcher m = DIGITS.matcher(input);
int len;
while (m.find()) {
len = m.group().length();
if (len == 9 || len == 16)
return false;
}
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.