Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.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/4/regex/19.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_Regex_Special Characters - Fatal编程技术网

如何查找字符串是否包含;只有";java中的特殊字符

如何查找字符串是否包含;只有";java中的特殊字符,java,regex,special-characters,Java,Regex,Special Characters,假设我将字符串定义为: String splChrs = "/-@#$%^&_-+=()" ; String inputString = getInputString(); //gets a String from end the user 我想检查字符串“inputString”是否只包含字符串“splChrs”中包含的特殊字符 i、 e.如果inputString只包含“splChrs”中的字符,我想将布尔标志设置为true 如果布尔标志包含“splChrs”中未包含

假设我将字符串定义为:

String splChrs      = "/-@#$%^&_-+=()" ;

String inputString = getInputString();  //gets a String from end the user
我想检查字符串“inputString”是否只包含字符串“splChrs”中包含的特殊字符 i、 e.如果inputString只包含“splChrs”中的字符,我想将布尔标志设置为true

如果布尔标志包含“splChrs”中未包含的任何字母,则将其设置为false

您可以使用:

String splChrs = "-/@#$%^&_+=()" ;
boolean found = inputString.matches("[" + splChrs + "]+");
PS:我已重新排列了
splChrs
变量中的字符,以确保连字符已开始避免转义。我还从字符串的中间删除了一个多余的(并且有问题的)连字符,否则它将赋予字符类不同的含义(表示范围)

感谢@Alanmore关于角色类中角色的说明:

^
如果先列出;]如果没有列在第一位
[
任何地方;如果紧跟其后的是另一个
&
,请尝试以下操作

if(inputString.matches("[" + splChrs + "]+")){

// set bool to true
}else{

// set bool to false
}

不要发明自行车,因为所有这些任务对每个人来说都很常见。 我建议在可能的情况下使用Apache Commons库:


包含

公共静态布尔值仅包含(字符串str, 字符[]有效)


String.matches(“[/-@#$%^&-+=()]+”)将为您解决此问题。您可能已经提到,您还删除了一个额外的连字符;我不得不以艰难的方式发现它。:P但连字符只是可能给您带来麻烦的几个字符中的一个。其他字符包括:
^
如果它首先列出;
]
如果它没有首先列出
[
任何地方;如果后面紧跟着另一个
&
,则为
&
。我知道示例中没有包含方括号,但它只是一个示例。您至少应该得到一个裁决。是的,忘记提及删除冗余(和有问题的)中间的连字符。Rest我检查了字符串中是否有其他字符需要转义,但没有找到其他大小写。很好的建议Alan,但是如果我想包含“[”?@shwetha:应该是:
splChrs=“-\[/@$%^&+=()”
对不起,我突然想到另一件事。那么“\”呢?请参阅下面关于此答案的许多潜在问题的我的评论。关于StringUtils的有用链接!
String splChrs = "-/@#$%^&_+=()" ;
boolean found = inputString.matches("[" + splChrs + "]+");
if(inputString.matches("[" + splChrs + "]+")){

// set bool to true
}else{

// set bool to false
}
Checks if the String contains only certain characters.

A null String will return false. A null valid character array will return false. An empty String (length()=0) always returns true.

 StringUtils.containsOnly(null, *)       = false
 StringUtils.containsOnly(*, null)       = false
 StringUtils.containsOnly("", *)         = true
 StringUtils.containsOnly("ab", '')      = false
 StringUtils.containsOnly("abab", 'abc') = true
 StringUtils.containsOnly("ab1", 'abc')  = false
 StringUtils.containsOnly("abz", 'abc')  = false


Parameters:
    str - the String to check, may be null
    valid - an array of valid chars, may be null 
Returns:
    true if it only contains valid chars and is non-null