我需要写一个正则表达式,用逗号分隔字符串,而不是用空格分隔字符串。我写了一个,但没有成功。
例如:
String testString = "CONGO, THE DEMOCRATIC REPUBLIC OF THE,IRAN, ISLAMIC REPUBLIC OF,KOREA, DEMOCRATIC PEOPLE S REPUBLIC OF,NEPAL,NEW ZEALAND,SRI LANKA";预期结果:
我的代码:
public class TestRegEx {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String testString = "CONGO, THE DEMOCRATIC REPUBLIC OF THE,IRAN, ISLAMIC REPUBLIC OF,KOREA, DEMOCRATIC PEOPLE S REPUBLIC OF,NEPAL,NEW ZEALAND,SRI LANKA";
String[] output = testString.split("([,][^(,\\s)])+");
for (String country : output) {
System.out.println(country);
}
}
}产出:
发布于 2013-10-20 17:02:31
,(?!\s)解释:
匹配没有空格的逗号。
在这里看到它的作用:http://regex101.com/r/gW3hJ8
发布于 2013-10-20 16:53:06
使用零宽度后视和前瞻
testString.split("(?<! ),(?! )")https://stackoverflow.com/questions/19480101
复制相似问题