Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中使用正则表达式检查字符串中的特殊字符_Java_Android_Regex - Fatal编程技术网

在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]
在线试用:

一个简单的方法是检查字符串是否有任何非字母数字 人物

试试这个,

StringChecker.java

结果如下所示:

$ 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("[$&+,:;=\\\\?@#|/'<>.^*()%!-]");