如果能确定要分隔的,号之前或之后有引号,则可以这么做。 String a = "1,\"22\",\"333,22\",\"32,22,22,23\",4,\"6\",5"; String[] b = a.split("\",|,\""); for(String s : b) System.out.println(s.replaceAll("\"", "")); 如果不是,那就只能再想想。
have a trypublic class Test { public static void main(String[] args) { String s = "1,\"22\",\"333,22\",\"32,22,22,23\",4,\"6\",5"; s = s.replaceAll("\",\"", "--"); //先去掉"," s = s.replaceAll(",\"", "--"); //再去掉," s = s.replaceAll("\",", "--"); //再去掉", System.out.println(s); } }
合在一起写 public class Test { public static void main(String[] args) { String s = "1,\"22\",\"333,22\",\"32,22,22,23\",4,\"6\",5"; s = s.replaceAll("\",\"|,\"|\",", "--"); //用| System.out.println(s); } }
String a = "1,\"22\",\"333,22\",\"32,22,22,23\",4,\"6\",5";
String[] b = a.split("\",|,\"");
for(String s : b)
System.out.println(s.replaceAll("\"", ""));
如果不是,那就只能再想想。
public static void main(String[] args) {
String s = "1,\"22\",\"333,22\",\"32,22,22,23\",4,\"6\",5";
s = s.replaceAll("\",\"", "--"); //先去掉","
s = s.replaceAll(",\"", "--"); //再去掉,"
s = s.replaceAll("\",", "--"); //再去掉",
System.out.println(s);
}
}
合在一起写
public class Test {
public static void main(String[] args) {
String s = "1,\"22\",\"333,22\",\"32,22,22,23\",4,\"6\",5";
s = s.replaceAll("\",\"|,\"|\",", "--"); //用|
System.out.println(s);
}
}
String str="1,\"22\",\"333,22\",\"32,22,22,23\",4,\"6\",5";
Pattern p2=Pattern.compile("(,(\\d+|\"(\\d+(,\\d+)*?)\"))");
System.out.println(p2.matcher(str).replaceAll("-$2").replaceAll("\"", ""));我没办法是用一个正则去掉引号,引号只好再用一个表达式去掉。
String reg = "(\\d+|\"(\\d+,)*\\d+\")(,)";
Matcher m = Pattern.compile(reg).matcher(str);
System.out.println(m.replaceAll("$1--"));
String str = "1,\"22\",\"333,22\",\"32,22,22,23\",4,\"6\",5";
String reg = "(\\d+|\"(\\d+,)*\\d+\")(,)";
Matcher m = Pattern.compile(reg).matcher(str);
System.out.println(m.replaceAll("$1--").replaceAll("\"", ""));
$text =~ s/(?:\",\"|,\"|\",)|,(?:(?![\d,]*\"))/--/g;
final String text = "1,\"22\",\"333,22\",\"32,22,22,23\",4,\"6\",5";
final String regex = "(?:\",\"|,\"|\",)|,(?:(?![\\d,]*\"))";
String result = Pattern.compile(regex).matcher(text).replaceAll("--");
final String text = "1,23,1,\"22\",\"333,22\",\"32,22,22,23\",4,4,4,\"6\",5,7,8,9";
final String regex = "(?:\",\"|,\"|\",)|,(?:(?![\\d,]*\",))";
String result = Pattern.compile(regex).matcher(text).replaceAll("--");这会应该没问题了
StringBuffer newStr= new StringBuffer("");
StringBuffer spliteStr = new StringBuffer("");
boolean errFlg=true;
for(char c : str.toCharArray()){
if(c>=48 && c<=59){
if(spliteStr.toString().equals(",") || spliteStr.toString().equals("")){
newStr.append(spliteStr);
}else {
newStr.append("--");
}
spliteStr.delete(0, spliteStr.length());
newStr.append(c);
}else{
spliteStr.append(c);
}
}
System.out.println(newStr);
s = s.replaceAll("\",\"|,\"|\",", "--").replaceAll("\"", ""); //这样就能去掉首尾"了
s = s.replaceAll("\",\"|,\"|\",", "--").replaceAll("^\\D*|\\D*$", "");
你说得很对,谢谢
修正版如下:
String str = "1,\"22\",\"333,22\",\"32,22,22,23\",4,\"6\",5";
String reg = "(,|^)(\\d+|\"(\\d+,)*\\d+\")";
Matcher m = Pattern.compile(reg).matcher(str);
System.out.println(m.replaceAll("--$2").replaceAll("\"", "").substring(2));
Pattern pat=Pattern.compile("\"([^\"]*)\"");
StringBuffer sb=new StringBuffer();
Matcher mcr=pat.matcher(str);
while(mcr.find()){
mcr.appendReplacement(sb, mcr.group(1).replace(",", "--"));
}
mcr.appendTail(sb);
str=sb.toString();
str=str.replaceAll("^\"|\"$", "");方法2.boolean b=false;
char[] cs=str.toCharArray();
StringBuilder sb=new StringBuilder();
for(char c:cs){
if(c=='"'){
b=!b;
continue;
}
if(c==','){
if(!b){
sb.append("--");
continue;
}
}
sb.append(c);
}
String[] tests = new String[] {
"1,2,\"22\",\"333,22\",\"32,22,22,23\",4,\"6\",5",
"\"1\",\"2,3,4\",5,6",
"\"1,2,3,4,5,6\"",
"1,3,5",
"1,\"1,2,3,4,5,6\"",
"1,\"1,2,3,4,5,6\",1,\"1,2,3,4,5,6\""
};
String regex =
"\\G(?:^|,) \n"
+ "(?: \n"
+ " # 要么是双引号字段... \n"
+ " \" # 字段起始双引号 \n"
+ " ( (?: [^\"]++ | \"\" )*+ ) \n"
+ " \" # 字段结束双引号 \n"
+ " |# ... 要么是 ... \n"
+ " # 非引号非逗号文本 ... \n"
+ " ( [^\",]*) \n"
+ " ) \n";
Pattern pattern = Pattern.compile(regex, Pattern.COMMENTS);
for (String test : tests) {
Matcher matcher = pattern.matcher(test);
System.out.println(test);
while (matcher.find()) {
String group1 = matcher.group(1);
String group2 = matcher.group(2);
String field = group1 == null ? group2 : group1;
System.out.println(field);
}
System.out.println("==========================");
}