我需要编写一个正则表达式来用逗号分隔字符串,但不能用空格分隔逗号。我写了一个,但没有成功。
例如:
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);
}
}
}
输出:
- 刚果民主共和国
- 伊朗伊斯兰共和国
- 奥里亚,民主人民共和国
- EPAL
- EW 新西兰
- 斯里兰卡