检查输入是否只包含某些字符

时间:2015-06-03 22:08:14

标签: java regex

我需要检查我的输入字符串是否只包含字母字符,星号,空格和制表符。

我试过这个,但它不起作用:

firstStr.matches(".*[a-zA-Z].*.*[\\t].*.*[\\s].*") 

2 个答案:

答案 0 :(得分:2)

使用此正则表达式检查字符串是否仅包含字母,空格,星号,标签:

<select id="elements">
    {% for elem in elements %}
        <option value='{{ elem }}'>{{ elem }}</option>\n
    {% endfor %}
</select>

你很接近,但你需要一个字符类。

答案 1 :(得分:0)

我相信这应该有效:

[\\*\\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