给出一个与正则表达式[A-Za-z !,?._'@]+
匹配的字符串,我想将字符串拆分为标记。令牌是一个或多个连续的英文字母。然后,我要打印令牌的数量。这是我的代码:
public class mySolution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s = scan.nextLine();
String [] parts = new String[1159];
parts = s.split("\\W");
System.out.println(parts.length);
for(int i = 0; i< parts.length; i++) {
System.out.println(parts[i]);
}
scan.close();
}
}
这是我尝试的输入:
He is a very very good boy, isn't he?
这是我收到的输出:
11
He
is
a
very
very
good
boy
isn
t
he
如您所见,一切都很好,但是令牌数量竟然是11个,而应该是10个!我相信非单词字符定界符"\W"
会将“逗号+空格”视为一个字符!我说的对吗?