[英]Check that the input only contains certain characters
I need to check that my input string only contains alphabetic characters, asterisks, spaces, and tabs. 我需要检查我的输入字符串仅包含字母字符,星号,空格和制表符。
I tried this, but it doesn't work: 我试过了,但是不起作用:
firstStr.matches(".*[a-zA-Z].*.*[\\t].*.*[\\s].*")
Use this regex to check if string contains only alphabets, spaces, asterisks, tabs: 使用此正则表达式检查字符串是否仅包含字母,空格,星号,制表符:
firstStr.matches("[a-zA-Z *\t]+")
You were close, but you needed a single character class. 你是接近,但你需要一个单一的字符类。
I believe this should work: 我相信这应该有效:
[\\*\\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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.