一个单词不应该以Java中的数字开头

时间:2013-06-02 21:03:54

标签: java regex

字符串应符合要求:

  
    
      

1.由一些用空格分隔的单词组成。

             

2.只允许每个单词中的字母,数字,下划线,'和'

             

3.每个单词必须以字母或引号开头

    
  

现在我写了这个并且它不符合最后的要求:

public boolean test(String string) {
return string.matches("[A-Za-z0-9_' \"]+");
}

如果我给出一个非法的字符串“这个世界以123digit开头”,它将返回false,但它返回true:

public boolean test("there is a word start with 123digit"){
\\my previous code return true.
\\so someone PLEASE HELP to implement the method so 
\\ it will return false in such situation.
}

也是这种情况,它应该返回false:

public boolean test("there is a word start with _underscore"){
\\my previous code return true.
\\so someone PLEASE HELP to implement the method so 
\\ it will return false in such situation.
}

1 个答案:

答案 0 :(得分:3)

string.matches("^([A-Za-z'\"][A-Za-z0-9_'\"]* *)+$")

Visualization