Java 检查输入是否仅包含某些字符
我需要检查输入字符串是否只包含字母、星号、空格和制表符 我试过这个,但不起作用:Java 检查输入是否仅包含某些字符,java,regex,Java,Regex,我需要检查输入字符串是否只包含字母、星号、空格和制表符 我试过这个,但不起作用: firstStr.matches(".*[a-zA-Z].*.*[\\t].*.*[\\s].*") 使用此正则表达式检查字符串是否仅包含字母、空格、星号和制表符: firstStr.matches("[a-zA-Z *\t]+") 您很接近,但需要一个单字符类。我相信这应该可以: [\\*\\t\\sa-zA-Z]+ //if you want to ensure that there is atleas
firstStr.matches(".*[a-zA-Z].*.*[\\t].*.*[\\s].*")
使用此正则表达式检查字符串是否仅包含字母、空格、星号和制表符:
firstStr.matches("[a-zA-Z *\t]+")
您很接近,但需要一个单字符类。我相信这应该可以:
[\\*\\t\\sa-zA-Z]+ //if you want to ensure that there is atleast 1 character in the string
[\\*\\t\\sa-zA-Z]* //if it is ok to have 0 or more characters in the string
\\* will match an asterisk
\\t will match tab
\\s will match whitespace
a-zA-Z will match alphabetical chars
[...]+ ensures there are atleast one of the expression in the brackets
[...]* means there is 0 or more of the expression in the brackets
阅读regex-docHey@Nick中的字符类,或许您可以提供一些
firstStr
的示例,以及您希望得到的结果。使用regex调试器在线帮助您如果这对您有效,请接受/upvote,以便其他人也可以看到