我有一个包含五个字段的csv字符串(3个强制字段和2个可选字段)。可选字段位于第3和第5位。 我有一个正则表达式模式来识别双引号中的字符串
private static final String TEXT_IN_DBL_QUOTES_REGEX="\"([^\"]*)\"";
private static final String rowData="\"Field1\", \"Field2\", , \"Field4\", ";
public static void main(String[] args){
List<String> fields = Lists.newArrayList();
Pattern regex = Pattern.compile(TEXT_IN_DBL_QUOTES_REGEX);
Matcher matcher = regex.matcher(rowData);
while(matcher.find()){
fields.add(matcher.group(1));
}
return fields;
}
My expected result is an array list containing
Field1, Field2, null, Field4, null
but the above is returning
Field1, Field2, Field4
我如何调整上述代码以获得所需的结果?
答案 0 :(得分:0)
你可以像这样调整你的正则表达式:
String TEXT_IN_DBL_QUOTES_REGEX="\"([^\"]*)\"|(?<=,) *(?=,|$)";
String rowData="\"Field1\", \"Field2\", , \"Field4\", ";
List<String> fields = new ArrayList<>();
Pattern regex = Pattern.compile(TEXT_IN_DBL_QUOTES_REGEX);
Matcher matcher = regex.matcher(rowData);
while(matcher.find()) {
fields.add(matcher.group(1));
}
System.out.println (fields);
<强>输出:强>
[Field1, Field2, null, Field4, null]