gpt4 book ai didi

java - 在引号外用逗号分割

转载 作者:行者123 更新时间:2023-12-02 23:21:35 27 4
gpt4 key购买 nike

我的程序从文件中读取一行。该行包含逗号分隔的文本,例如:

123,test,444,"don't split, this",more test,1

我希望分割的结果是这样的:

123
test
444
"don't split, this"
more test
1

如果我使用String.split(","),我会得到这个:

123
test
444
"don't split
this"
more test
1

换句话说:子字符串“don't split, this”中的逗号不是分隔符。怎么处理这个问题?

最佳答案

您可以尝试这个正则表达式:

str.split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");

这会在 , 上分割字符串,后跟偶数个双引号。换句话说,它在双引号外以逗号分隔。如果您的字符串中有平衡的引号,这将起作用。

说明:

,           // Split on comma
(?= // Followed by
(?: // Start a non-capture group
[^"]* // 0 or more non-quote characters
" // 1 quote
[^"]* // 0 or more non-quote characters
" // 1 quote
)* // 0 or more repetition of non-capture group (multiple of 2 quotes will be even)
[^"]* // Finally 0 or more non-quotes
$ // Till the end (This is necessary, else every comma will satisfy the condition)
)

您甚至可以在代码中输入这样的内容,使用 (?x) 修饰符和正则表达式。修饰符会忽略正则表达式中的任何空格,因此更容易阅读分成多行的正则表达式,如下所示:

String[] arr = str.split("(?x)   " + 
", " + // Split on comma
"(?= " + // Followed by
" (?: " + // Start a non-capture group
" [^\"]* " + // 0 or more non-quote characters
" \" " + // 1 quote
" [^\"]* " + // 0 or more non-quote characters
" \" " + // 1 quote
" )* " + // 0 or more repetition of non-capture group (multiple of 2 quotes will be even)
" [^\"]* " + // Finally 0 or more non-quotes
" $ " + // Till the end (This is necessary, else every comma will satisfy the condition)
") " // End look-ahead
);

关于java - 在引号外用逗号分割,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50492142/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com