在我的文件中,我有以下数据,一切都是字符串
输入
"abcd","12345","success,1234,out",,"hai"
输出应如下所示
第1栏:" abcd"
第2栏:" 12345"
第3栏:"成功,1234,out"
第4栏:null
第5栏:" hai"
我们需要使用逗号作为分隔符,空值没有双引号。
你能不能帮我找一个解析这个数据的正则表达式
答案 0 :(得分:1)
您可以尝试使用OpenCsv https://sourceforge.net/projects/opencsv/
中的.widget {
page-break-inside: avoid;
}
工具
您甚至可以配置CSVReader
(由读者使用)在几个条件下输出CSVParser
。来自doc:
null
答案 1 :(得分:0)
您可以使用此正则表达式
"([^"]*)"
DEMO:https://regex101.com/r/WpgU9W/1
Match 1
Group 1. 1-5 `abcd`
Match 2
Group 1. 8-13 `12345`
Match 3
Group 1. 16-32 `success,1234,out`
Match 4
Group 1. 36-39 `hai`
答案 2 :(得分:0)
使用("[^"]+")|(?<=,)(,)
正则表达式,您可以找到引用的字符串("[^"]+"
),其应被视为原样,或逗号前面的逗号,表示空字段值。您现在需要的只是迭代匹配并检查定义的两个捕获组中的哪一个并相应地输出:
String input = "\"abcd\",\"12345\",\"success,1234,out\",,\"hai\"";
Pattern pattern = Pattern.compile("(\"[^\"]+\")|(?<=,)(,)");
Matcher matcher = pattern.matcher(input);
int col = 1;
while (matcher.find()) {
if (matcher.group(1) != null) {
System.out.println("Column " + col + ": " + matcher.group(1));
col++;
} else if (matcher.group(2) != null) {
System.out.println("Column " + col + ": null");
col++;
}
}
答案 3 :(得分:0)
步骤1:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
final String regex = "(,,)";
final String string = "\"abcd\",\"12345\",\"success,1234,out\",,\"hai\"\n"
+ "\"abcd\",\"12345\",\"success,1234,out\",\"null\",\"hai\"";
final String subst = ",\"null\",";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);
System.out.println("Substitution result: " + result);
原文:
"abcd","12345","success,1234,out",,"hai"
转化:(有效)
"abcd","12345","success,1234,out","null","hai"
步骤2 :(使用REGEXP)
"([^"]*)"
<强>结果:强>
abcd
12345
success,1234,out
null
hai
现金:
Emmanuel Guiton [https://stackoverflow.com/users/7226842/emmanuel-guiton] REGEXP
答案 4 :(得分:0)
您也可以使用替换功能:
final String inuput = "\"abcd\",\"12345\",\"success,1234,out\",,\"hai\"";
System.out.println(inuput);
String[] strings = inuput
.replaceAll(",,", ",\"\",")
.replaceAll(",,", ",\"\",") // if you have more then one null successively
.replaceAll("\",\"", "\";\"")
.replaceAll("\"\"", "")
.split(";");
for (String string : strings) {
String output = string;
if (output.isEmpty()) {
output = null;
}
System.out.println(output);
}