[英]Java - what is the best way to check if a STRING contains only certain characters?
[英]Check that the input only contains certain characters
我需要檢查我的輸入字符串僅包含字母字符,星號,空格和制表符。
我試過了,但是不起作用:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.