2015-07-21 51 views
0

我需要在字符串中,只有这些标点字符应当被忽略这样的方式分割:JAVA正则表达式:让自定义的正则表达式String.split()

),() (!)和(?)

所以,如果我有这样的字符串:

String a = "I can't split this string! Guys, can you help me? Thanks in advance." 

我有这样的重GEX(它没有帮助):

String.split("[\\p{Punct}\\s]+"); 

,并从它的输出是:

 
I 
can 
t 
split 
this 
string 
Guys 
can 
you 
help 
me 
Thanks 
in 
advance 

如果您发现不能被分割,因为那一撇是作为一个标点符号,和我不想成为。

+0

目前还不清楚你在问什么。什么是期望的行为? –

回答

2
public static void main(String[] args) 
    { 
    String a = "I can't split this string! Guys, can you help me? Thanks in advance."; 

    String[] splitted = a.split("[.,!?\\s]+"); 
    for (String s : splitted) 
    { 
     System.out.println(s); 
    } 
    } 

删除\\s如果你不想用空格

0
String.split(" |,|\\?|!|\\.") 
OR 
String.split("[\\s\\.,!?]+") 
0

我已经找到另一种不同的方式,它起分裂。

String.split("[\\?\\!\\.\\,\\s]+"); 
0

需要更新双线

String Str1 = "I can't split this string! Guys, can you help me? Thanks in advance."; 
String[] splite = Str1.split("[.,!?\\s]+"); 
0

可避免对单引号分割,而在其他所有标点字符或空间拆分:

String a = "I can't split this string! Guys, can you help me? Thanks in advance." 
String[] toks = a.split("[\\s\\p{Punct}&&[^']]+");