Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 正则表达式,匹配非字符一次或多次_Java_Regex - Fatal编程技术网

Java 正则表达式,匹配非字符一次或多次

Java 正则表达式,匹配非字符一次或多次,java,regex,Java,Regex,我对这段代码有问题,特别是while循环中的代码 public void mSetSafeCode() // Manually define safe passcode { int mIntPasscode; String mStringPasscode; Scanner sc = new Scanner(System.in); System.out.print("Enter your desired numerical passcode (max 3 digi

我对这段代码有问题,特别是while循环中的代码

public void mSetSafeCode() // Manually define safe passcode
{
    int mIntPasscode;
    String mStringPasscode;
    Scanner sc = new Scanner(System.in);

    System.out.print("Enter your desired numerical passcode (max 3 digits): ");
    mStringPasscode = sc.nextLine();
    while(mStringPasscode.matches("\\D+") || mStringPasscode.length() > 3 || mStringPasscode.length() < 3) // If input not digit or exceeds length
    {
        System.out.print("Error! You inputted an invalid passcode, try again: ");
        mStringPasscode = sc.nextLine(); // Prints error, gets user to input again
    }
    mIntPasscode = Integer.parseInt(mStringPasscode); // If while is not met, parses input into an integer
    System.out.println("You've set the numerical passcode to " + mIntPasscode); // Prints the passcode
}
当用户从扫描仪输入的字符不是数字时,我试图这样做,以抛出错误。我相信我的正则表达式对于\\D+是正确的,但是如果我使用'f33'作为输入,它就不会被while循环捕获。我想这是因为我使用了一个或,字符串长度,但如果它包含一个字符而不是一个数字,或者它的长度大于/小于3,我希望它被while循环捕获

谢谢你的帮助

在while循环中,您有:

while(mStringPasscode.matches("\\D+")
你希望它是:

while(!mStringPasscode.matches("\\d+")
说明:

\\D+匹配非数字,但仅当整个字符串为非数字时才匹配。您希望使用\\d+,如果字符串中的任何字符是非数字字符,它将返回false。匹配整个字符串,因此如果有一个字符不匹配,它将返回false

样本输出:

在while循环中,您有:

while(mStringPasscode.matches("\\D+")
你希望它是:

while(!mStringPasscode.matches("\\d+")
说明:

\\D+匹配非数字,但仅当整个字符串为非数字时才匹配。您希望使用\\d+,如果字符串中的任何字符是非数字字符,它将返回false。匹配整个字符串,因此如果有一个字符不匹配,它将返回false

样本输出:


使用[0-9]{3}将导致仅在3位数字上匹配。[\\d]{3}也会起作用。

使用[0-9]{3}只会导致3位数字的匹配。[\\d]{3}也可以使用。

如果只允许3个数字,则最好是写入

[0-9]{1,3}

如果您只允许使用3个数字,则最好是书写

[0-9]{1,3}

我以为\\D是非数字,而\\D是数字?@josef抱歉,我的答案有点不清楚。为Clarity编辑谢谢,现在有意义了!因此,\\D+比较整个字符串,而\\D+检查字符串中的任何字符,对吗?@josef matches扫描整个字符串,因此无论模式如何,如果有一个字符不匹配,它都会返回falseOK So!mStringPasscode.matches\\d+检查整个字符串是否只包含数字以外的任何内容?我以为\\d不是数字,而\\d是数字?@josef抱歉,我的答案有点不清楚。为Clarity编辑谢谢,现在有意义了!因此,\\D+比较整个字符串,而\\D+检查字符串中的任何字符,对吗?@josef matches扫描整个字符串,因此无论模式如何,如果有一个字符不匹配,它都会返回falseOK So!mStringPasscode.matches\\d+检查整个字符串是否只包含数字以外的任何内容?不确定\\d周围是否需要括号,尽管我发现\\d{3}有效。这是更容易阅读,虽然它减少了长度的需要,所以谢谢你。匹配任何字符,Astrik是一个贪婪的量词,指定要查找的任何字符中的零个或多个。因此,通过在非数字字符类的两侧,这确保了字母字符可以被数字或其他数字包围。网上有很多关于量词的好资源。另外,请查看Java正则表达式planet,它就像一个小沙箱,用于尝试各种正则表达式。虽然我发现\\d{3}有效,但我不确定\\d周围是否需要括号。这是更容易阅读,虽然它减少了长度的需要,所以谢谢你。匹配任何字符,Astrik是一个贪婪的量词,指定要查找的任何字符中的零个或多个。因此,通过在非数字字符类的两侧,这确保了字母字符可以被数字或其他数字包围。网上有很多关于量词的好资源。另外,请查看Java正则表达式planet,它就像一个小沙箱,用于尝试各种正则表达式。当你学习的时候。