如果在我的String中两个正则表达式是连续的[逗号(,)和space()],那么我想处理一个正则表达式,该怎么办。

String s = "He is a very very good boy, isn't he?";
String str[]=s.split("[!._,'@? ]");


我的输出是:-

11
He
is
a
very
very
good
boy
space( )
isn
t
he


我要这个:-

10
He
is
a
very
very
good
boy
isn
t
he

最佳答案

您也可以使用StringTokenizer获得相同的结果。
这是代码段:-

StringTokenizer str = new StringTokenizer(s," !@,.?_'");
    System.out.println(str.countTokens());
    while(str.hasMoreTokens()) {
        System.out.println(str.nextToken());
}


要了解StringTokenizer和split()之间的区别,请检查此link

关于java - 拆分方法中使用多个正则表达式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57749020/

10-08 23:47