![](/img/trans.png)
[英]regex Java splitting a comma-separated String but ignoring commas within quotes+braces+recursive brackets
[英]Java: splitting a comma-separated string but ignoring commas in quotes
我有一個模糊的字符串:
foo,bar,c;qual="baz,blurb",d;junk="quux,syzygy"
我想用逗號分隔 - 但我需要忽略引號中的逗號。 我怎樣才能做到這一點? 似乎正則表達式方法失敗了; 我想我可以在看到報價時手動掃描並輸入不同的模式,但是使用預先存在的庫會很好。 (編輯:我想我的意思是已經是 JDK 的一部分或者已經是 Apache Commons 等常用庫的一部分的庫。)
上面的字符串應該分成:
foo
bar
c;qual="baz,blurb"
d;junk="quux,syzygy"
注意:這不是 CSV 文件,它是包含在具有更大整體結構的文件中的單個字符串
嘗試:
public class Main {
public static void main(String[] args) {
String line = "foo,bar,c;qual=\"baz,blurb\",d;junk=\"quux,syzygy\"";
String[] tokens = line.split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", -1);
for(String t : tokens) {
System.out.println("> "+t);
}
}
}
輸出:
> foo
> bar
> c;qual="baz,blurb"
> d;junk="quux,syzygy"
換句話說:僅當逗號前面有零個或偶數個引號時才拆分逗號。
或者,對眼睛更友好一點:
public class Main {
public static void main(String[] args) {
String line = "foo,bar,c;qual=\"baz,blurb\",d;junk=\"quux,syzygy\"";
String otherThanQuote = " [^\"] ";
String quotedString = String.format(" \" %s* \" ", otherThanQuote);
String regex = String.format("(?x) "+ // enable comments, ignore white spaces
", "+ // match a comma
"(?= "+ // start positive look ahead
" (?: "+ // start non-capturing group 1
" %s* "+ // match 'otherThanQuote' zero or more times
" %s "+ // match 'quotedString'
" )* "+ // end group 1 and repeat it zero or more times
" %s* "+ // match 'otherThanQuote'
" $ "+ // match the end of the string
") ", // stop positive look ahead
otherThanQuote, quotedString, otherThanQuote);
String[] tokens = line.split(regex, -1);
for(String t : tokens) {
System.out.println("> "+t);
}
}
}
產生與第一個示例相同的結果。
正如@MikeFHay 在評論中提到的:
我更喜歡使用Guava 的 Splitter ,因為它具有更合理的默認值(參見上面關於
String#split()
修剪空匹配的討論,所以我這樣做了:Splitter.on(Pattern.compile(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)"))
雖然我一般都喜歡正則表達式,但對於這種依賴於狀態的標記化,我相信一個簡單的解析器(在這種情況下比這個詞聽起來更簡單)可能是一個更干凈的解決方案,特別是在可維護性方面,例如:
String input = "foo,bar,c;qual=\"baz,blurb\",d;junk=\"quux,syzygy\"";
List<String> result = new ArrayList<String>();
int start = 0;
boolean inQuotes = false;
for (int current = 0; current < input.length(); current++) {
if (input.charAt(current) == '\"') inQuotes = !inQuotes; // toggle state
else if (input.charAt(current) == ',' && !inQuotes) {
result.add(input.substring(start, current));
start = current + 1;
}
}
result.add(input.substring(start));
如果您不關心保留引號內的逗號,則可以通過將引號中的逗號替換為其他內容然后以逗號分隔來簡化此方法(不處理起始索引,不處理最后一個字符的特殊情況):
String input = "foo,bar,c;qual=\"baz,blurb\",d;junk=\"quux,syzygy\"";
StringBuilder builder = new StringBuilder(input);
boolean inQuotes = false;
for (int currentIndex = 0; currentIndex < builder.length(); currentIndex++) {
char currentChar = builder.charAt(currentIndex);
if (currentChar == '\"') inQuotes = !inQuotes; // toggle state
if (currentChar == ',' && inQuotes) {
builder.setCharAt(currentIndex, ';'); // or '♡', and replace later
}
}
List<String> result = Arrays.asList(builder.toString().split(","));
http://sourceforge.net/projects/javacsv/
https://github.com/pupi1985/JavaCSV-Reloaded (前一個庫的分支,允許生成的輸出在不運行 Windows 時具有 Windows 行終止符\r\n
)
http://opencsv.sourceforge.net/
我不建議 Bart 給出正則表達式的答案,我發現在這種特殊情況下解析解決方案更好(正如 Fabian 建議的那樣)。 我已經嘗試過正則表達式解決方案和自己的解析實現,我發現:
我的解決方案和測試如下。
String tested = "foo,bar,c;qual=\"baz,blurb\",d;junk=\"quux,syzygy\",";
long start = System.nanoTime();
String[] tokens = tested.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
long timeWithSplitting = System.nanoTime() - start;
start = System.nanoTime();
List<String> tokensList = new ArrayList<String>();
boolean inQuotes = false;
StringBuilder b = new StringBuilder();
for (char c : tested.toCharArray()) {
switch (c) {
case ',':
if (inQuotes) {
b.append(c);
} else {
tokensList.add(b.toString());
b = new StringBuilder();
}
break;
case '\"':
inQuotes = !inQuotes;
default:
b.append(c);
break;
}
}
tokensList.add(b.toString());
long timeWithParsing = System.nanoTime() - start;
System.out.println(Arrays.toString(tokens));
System.out.println(tokensList.toString());
System.out.printf("Time with splitting:\t%10d\n",timeWithSplitting);
System.out.printf("Time with parsing:\t%10d\n",timeWithParsing);
當然,如果您對它的丑陋感到不舒服,您可以自由地將 switch 更改為此代碼段中的 else-ifs。 請注意,使用分隔符切換后缺少中斷。 StringBuilder 被設計用來代替 StringBuffer 以提高速度,其中線程安全無關緊要。
您處於正則表達式幾乎不會做的那個煩人的邊界區域(正如 Bart 指出的那樣,轉義引號會使生活變得艱難),但是成熟的解析器似乎有點矯枉過正。
如果您可能很快需要更大的復雜性,我會去尋找一個解析器庫。 比如 這個
我很不耐煩,選擇不等待答案......作為參考,做這樣的事情看起來並不難(這適用於我的應用程序,我不需要擔心轉義引號,因為引號中的東西僅限於一些受約束的形式):
final static private Pattern splitSearchPattern = Pattern.compile("[\",]");
private List<String> splitByCommasNotInQuotes(String s) {
if (s == null)
return Collections.emptyList();
List<String> list = new ArrayList<String>();
Matcher m = splitSearchPattern.matcher(s);
int pos = 0;
boolean quoteMode = false;
while (m.find())
{
String sep = m.group();
if ("\"".equals(sep))
{
quoteMode = !quoteMode;
}
else if (!quoteMode && ",".equals(sep))
{
int toPos = m.start();
list.add(s.substring(pos, toPos));
pos = m.end();
}
}
if (pos < s.length())
list.add(s.substring(pos));
return list;
}
(讀者練習:通過查找反斜杠擴展到處理轉義的引號。)
嘗試像(?!\"),(?!\")
這樣的環顧四周。 這應該匹配,
沒有被"
包圍。
最簡單的方法是不使用復雜的附加邏輯來匹配分隔符,即逗號,以匹配實際預期的內容(可能是引用字符串的數據),只是為了排除錯誤的分隔符,而是首先匹配預期的數據。
該模式由兩個替代方案組成,帶引號的字符串( "[^"]*"
或".*?"
)或直到下一個逗號的所有內容( [^,]+
)。為了支持空單元格,我們必須允許未加引號的項目為空並使用下一個逗號(如果有),並使用\\G
錨:
Pattern p = Pattern.compile("\\G\"(.*?)\",?|([^,]*),?");
該模式還包含兩個捕獲組以獲取引用字符串的內容或純內容。
然后,使用 Java 9,我們可以得到一個數組
String[] a = p.matcher(input).results()
.map(m -> m.group(m.start(1)<0? 2: 1))
.toArray(String[]::new);
而較舊的 Java 版本需要一個循環
for(Matcher m = p.matcher(input); m.find(); ) {
String token = m.group(m.start(1)<0? 2: 1);
System.out.println("found: "+token);
}
將項目添加到List
或數組中,留給讀者作為消費稅。
對於 Java 8,您可以使用此答案的results()
實現,就像 Java 9 解決方案一樣。
對於帶有嵌入字符串的混合內容,例如問題中,您可以簡單地使用
Pattern p = Pattern.compile("\\G((\"(.*?)\"|[^,])*),?");
但是,字符串會保留在引用的形式中。
正則表達式無法處理轉義字符。 對於我的應用程序,我需要能夠轉義引號和空格(我的分隔符是空格,但代碼是相同的)。
這是我在 Kotlin(這個特定應用程序的語言)中的解決方案,基於 Fabian Steeg 的解決方案:
fun parseString(input: String): List<String> {
val result = mutableListOf<String>()
var inQuotes = false
var inEscape = false
val current = StringBuilder()
for (i in input.indices) {
// If this character is escaped, add it without looking
if (inEscape) {
inEscape = false
current.append(input[i])
continue
}
when (val c = input[i]) {
'\\' -> inEscape = true // escape the next character, \ isn't added to result
',' -> if (inQuotes) {
current.append(c)
} else {
result += current.toString()
current.clear()
}
'"' -> inQuotes = !inQuotes
else -> current.append(c)
}
}
if (current.isNotEmpty()) {
result += current.toString()
}
return result
}
我認為這不是使用正則表達式的地方。 與其他觀點相反,我認為解析器並不過分。 它大約有 20 行,而且相當容易測試。
與其使用前瞻和其他瘋狂的正則表達式,不如先拉出引號。 也就是說,對於每個引用分組,用__IDENTIFIER_1
或其他指示符替換該分組,並將該分組映射到字符串、字符串的映射。
用逗號分割后,將所有映射的標識符替換為原始字符串值。
使用 String.split() 的單線如何?
String s = "foo,bar,c;qual=\"baz,blurb\",d;junk=\"quux,syzygy\"";
String[] split = s.split( "(?<!\".{0,255}[^\"]),|,(?![^\"].*\")" );
我會做這樣的事情:
boolean foundQuote = false;
if(charAtIndex(currentStringIndex) == '"')
{
foundQuote = true;
}
if(foundQuote == true)
{
//do nothing
}
else
{
string[] split = currentString.split(',');
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.