简体   繁体   English

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

[英]Trying to check if string contains special characters or lowercase java

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. 我的目标是从Scanner中读取一个字符串,然后运行此功能。 If the string has either lower case values OR special characters then I want to call the invalid function and then return NULL. 如果字符串具有小写值或特殊字符,则我想调用无效函数,然后返回NULL。 Then in the isValid() method, I'll have it return false and end. 然后在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. 如果它仅包含NUMBERS和UPPERCASE字符,我想按原样返回该字符串,以便它可以执行其他操作。

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;
    }

matches will try matching from start of string.If at the start there is not lowercase or Special characters it will fail .Use .find or simply make positive assertion. matches将尝试从匹配start在string.If的start没有lowercaseSpecial characters就会fail 。使用.find或者干脆做出了积极的断言。

^[A-Z0-9]+$

If this passes with matches its a valid string for you. 如果passesmatches的为您有效的字符串。

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 转义的Java字符串版本
of: az~@#$%^&*:;<>.,/}{+ of: az~@#$%^&*:;<>.,/}{+
is: "az~@#\\\\$%\\\\^&\\\\*:;<>\\\\.,/}\\\\{\\\\+" 是: "az~@#\\\\$%\\\\^&\\\\*:;<>\\\\.,/}\\\\{\\\\+"

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;
}

Your regex will match a string that only has 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java Spring 表单检查输入字符串是否包含特殊字符 - Java Spring form check whether a input string contains special characters or not 检查字符串是否包含特殊字符(句点和问号Java除外) - Check if a string contains special characters except period and question mark Java 检查子字符串是否在忽略大写、小写和特殊字符的字符串中? - Check If a substring is in a string ignoring uppercase, lowercase and special characters? 如何检查字符串是否包含任何特殊字符? - How to check is string contains any of special characters? Java Spring表单验证器,用于检查输入字符串是否包含特殊字符 - Java Spring form Validator to check whether a input string contains special characters or not java:检查带有特殊字符的字符串(少数除外) - java: Check string with special characters except few 如何检查字符串是否包含小写字母,大写字母,特殊字符和数字? - How to check whether a string contains lowercase letter, uppercase letter, special character and digit? 将包含土耳其语字符的字符串转换为小写 - Converting String which contains Turkish characters to lowercase Android(java)拆分字符串,其中包含特殊字符? - Android ( java ) split String which Contains Special Characters? 如何查找字符串是否包含“仅”特殊字符在java中 - How to find if a string contains “ONLY” Special characters in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM