-7

字符串应满足要求:

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

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

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

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

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

如果我给出一个非法字符串 "this world start with 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.
}
4

1 回答 1

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

可视化

于 2013-06-02T21:08:50.780 回答