在java中使用正则表达式检查字符串中的特殊字符
如何检查字符串中的特殊字符?我正在使用正则表达式检查是否只有空格,但当我输入特殊字符时,它会将它们视为空格。下面是我的代码在java中使用正则表达式检查字符串中的特殊字符,java,android,regex,Java,Android,Regex,如何检查字符串中的特殊字符?我正在使用正则表达式检查是否只有空格,但当我输入特殊字符时,它会将它们视为空格。下面是我的代码 private boolean emptySpacecheck(String msg){ return msg.matches(".*\\w.*"); } 如何检查特殊字符?您可以使用模式匹配器检查特殊字符,您可以检查以下示例: Pattern regex = Pattern.compile("[$&+,:;=\\\\?@#|/'<>.^
private boolean emptySpacecheck(String msg){
return msg.matches(".*\\w.*");
}
如何检查特殊字符?您可以使用模式匹配器检查特殊字符,您可以检查以下示例:
Pattern regex = Pattern.compile("[$&+,:;=\\\\?@#|/'<>.^*()%!-]");
if (regex.matcher(your_string).find()) {
Log.d("TTT, "SPECIAL CHARS FOUND");
return;
}
Pattern regex=Pattern.compile(“[$&+,:;=\\\\?@\\\\\\\\\\\?@\\\\\\\\/”。^*()%!-”;
if(regex.matcher(您的_字符串).find()){
Log.d(“TTT,发现的特殊字符”);
返回;
}
希望这能帮助你…如果你需要任何帮助,你可以问
…当我输入特殊字符时,它将它们视为空间
这意味着您只想检查字符串是否包含空格
您不需要正则表达式来检查空间,只需调用方法即可
您可以使用以下RegExp:
private boolean emptySpacecheck(String msg){
return msg.matches(".*\\s+.*");
}
与以下字符匹配:\s
[\t\n\x0B\f\r]
$ java SpecialChars
abc$def^ghi#jkl
position 3: $
position 7: ^
position 11: #
There are 3 special characters
您可以根据需要在编译方法中将自己的模式作为param传递
要检查特殊字符,请执行以下操作:
Pattern.compile("[$&+,:;=\\\\?@#|/'<>.^*()%!-]");
Pattern.compile(“[$&+,:;=\\\\?@\\\\\\\\\\\?@\\\\\\\\\\/”。^*()%!-”;
空白是
\s
而不是\w
。
$ java SpecialChars
abc$def^ghi#jkl
position 3: $
position 7: ^
position 11: #
There are 3 special characters
Pattern.compile("[$&+,:;=\\\\?@#|/'<>.^*()%!-]");