试图检查字符串是否包含特殊字符或小写java [英] Trying to check if string contains special characters or lowercase java

查看:164
本文介绍了试图检查字符串是否包含特殊字符或小写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".

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

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.

如果它只包含NUMBERS和UPPERCASE字符,我想返回字符串,因为它可以做其他事情。

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.

感谢您的帮助。

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


推荐答案

匹配将尝试匹配来自 start 的字符串。如果 start 则没有小写特殊字符它将失败。使用 .find 或只是做出肯定的断言。

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.

^[A-Z0-9]+$

如果通过 匹配它是一个有效的字符串。

If this passes with matches its a valid string for you.

这篇关于试图检查字符串是否包含特殊字符或小写java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆