简体   繁体   English

允许字符串中除9位和16位以外的任何数字

[英]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. 现在他们有两个问题。

and see here . 并看到这里

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.

相关问题 检查字符串是否仅包含数字,然后仅在不包含数字以外的字符时进行设置 - Checking if a string contains only digits and then setting it only if it doesn't contain characters other than digits 测试字符串是否与集合中的其他字符串不同 - Testing if a string is different than any other in a set 正则表达式只允许使用10或16位逗号分隔的数字 - Regex to allow only 10 or 16 digit comma separated number 除了'string [] args'之外,java的main方法中还有其他参数吗? - Is there any other parameter in the main method of java other than 'string[] args'? 正则表达式 - 允许空格和任意位数 - Regular expression - allow space and any number of digits 递归方法,打印的数字大于或等于第一个数字且低于最后一个数字 - Recursive method that prints all of the digits in a number that or greater than the first digit and lower than the last digit 检查 Java 中两个三位数是否有相同的数字 - Checking if two three digit numbers have any of there digits that are the same in Java Java StringToNUMBER - 如果字符串包含数字以外的任何内容和负数的初始“-”,则返回 0 - Java StringToNUMBER - return 0 if the string contains anything other than digits and an initial "-" for negative numbers Java Regex接受除破折号以外的任何字符串 - Java Regex that accept any string other than all dashes 如何检查字符串中是否包含数字以外的其他字符? - how to check string contain any character other than number in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM