尝试检查字符串是否包含特殊字符或小写java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28873407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 06:58:28  来源:igfitidea点击:

Trying to check if string contains special characters or lowercase java

javaregexstringobject

提问by magna_nz

I'm trying to get this regex lines to work but they don't seem to be working (I can't get it print out "matches".

我试图让这个正则表达式行工作,但他们似乎没有工作(我无法打印出“匹配项”。

My goal is to read in a string from Scanner and then run this function. If the string has either lower case values OR special characters then I want to call the invalid function and then return NULL. Then in the isValid() method, I'll have it return false and end.

我的目标是从 Scanner 读入一个字符串,然后运行这个函数。如果字符串具有小写值或特殊字符,那么我想调用无效函数,然后返回 NULL。然后在 isValid() 方法中,我会让它返回 false 并结束。

if it contains NUMBERS and UPPERCASE characters only i want to return the string as it is so it can do other things.

如果它只包含数字和大写字符,我想按原样返回字符串,以便它可以做其他事情。

I can't seem to get it print out "matches". I'm sure I'm doing this right and it's really frustrating me, I've been checking the forums for different ways but none seem to work.

我似乎无法打印出“匹配项”。我确信我这样做是正确的,这让我很沮丧,我一直在查看论坛的不同方式,但似乎都没有奏效。

Thanks for the help.

谢谢您的帮助。

  public static String clean(String str){

    String regex = "a-z~@#$%^&*:;<>.,/}{+";
    if (str.matches("[" + regex + "]+")){
        printInvalidString(str);
        System.out.println("matches");
    } else{
        return str;
    }

    return null;
}

public static boolean isValid(String validationString){

    //clean the string
    validationString = clean(validationString);
    if (validationString == null){
        return false;
    }

采纳答案by vks

matcheswill try matching from startof string.If at the startthere is not lowercaseor Special charactersit will fail.Use .findor simply make positive assertion.

matches将尝试匹配 from startof string.If at the startthere is not lowercaseor Special charactersit will fail.Use.find或简单地做出肯定的断言。

^[A-Z0-9]+$

If this passeswith matchesits a valid string for you.

如果这passesmatches一个对你有效的字符串。

回答by assylias

Your regex will match a string that onlyhas invalid characters. So a string with valid and invalid characters will not match the regex.

您的正则表达式将匹配一个只有无效字符的字符串。因此具有有效和无效字符的字符串将与正则表达式不匹配。

Validating the strings would be easier:

验证字符串会更容易:

if (str.matches([\dA-Z]+)) return str;
else printInvalidString(str);

回答by Arian

beside checking the string with long pattern you can check it if contain uppercase or numbers i rewrite the function in this way:

除了检查带有长模式的字符串之外,您还可以检查它是否包含大写或数字,我以这种方式重写了函数:

 public static String clean(String str) {

    //String regex = "a-z~@#$%^&*:;<>.,/}{+";
    Pattern regex=Pattern.compile("[^A-Z0-9]");
    if (str.matches(".*" + regex + ".*")) {
        printInvalidString(str);
        System.out.println("matches");
    } else {
        return str;
    }

    return null;
}

回答by Andie2302

To match numbers and upper case characters only use:

匹配数字和大写字符只使用:

^[\p{Lu}\p{Nd}]+$

`^`      ... Assert position is at the beginning of the string.
`[`      ... Start of the character class
`\p{Lu}` ... Match an "uppercase letter"
`\p{Nd}` ... Match a "decimal digit"
`]`      ... End of the character class
`+`      ... Match between 1 and unlimited times.
`$`      ... Assert position is at the end of the string.


The escaped java string version
of: a-z~@#$%^&*:;<>.,/}{+
is: "a-z~@#\\$%\\^&\\*:;<>\\.,/}\\{\\+"

转义的 java 字符串版本
a-z~@#$%^&*:;<>.,/}{+
是:"a-z~@#\\$%\\^&\\*:;<>\\.,/}\\{\\+"