我需要编写一个正则表达式来用逗号分隔一个字符串,而不是用空格逗号。我写了一个,但它没有用完。
E.g:
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);
}
}
}
输出:
答案 0 :(得分:3)
答案 1 :(得分:2)
使用zero width lookbehind and lookahead
testString.split("(?<! ),(?! )")