[英]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
没有lowercase
或Special characters
就会fail
。使用.find
或者干脆做出了积极的断言。
^[A-Z0-9]+$
If this passes
with matches
its a valid string for you. 如果
passes
与matches
的为您有效的字符串。
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.