我有一個像hello, "darkness, my", (old, friend)
一個字符串,我想這個分裂的結果:
hello
"darkness, my"
(old, friend)
分割字符串用逗號而忽略括號或引號逗號
我發現了一個辦法忽略逗號。在「-marks(,?=([^\"]*\"[^\"]*\")*[^\"]*$
),另一個辦法忽略括號中的逗號(,(?=[^\\)]*(?:\\(|$))
)
當我使用的第一個,我得到:
hello
"darkness, my"
(old
friend)
當我使用第二個,我得到:
hello
"darkness
my"
(old, friend)
但我怎麼結合這兩種解決方案?
變異[**最佳的正則表達式特技以往**](http://www.rexegg.com/regex-best -trick.html) - 向下滾動到「Java」部分。 – Jan
您可以隨時使用兩種測試...,(?= ...)(?= ...)'。或者爲了簡化你的生活,不要使用描述分隔符的split,使用'Matcher#find'並描述你對'任何單詞'或'引號之間的文本'''OR'(括號之間的文本)'感興趣的標記。或者甚至更好的是不要使用正則表達式。只需遍歷字符,並注意括號或引號之外的逗號,如https://stackoverflow.com/questions/12756651/split-a-string-based-on-regex/12756948#12756948 – Pshemo
謝謝你們兩位。這是一個非常小的事情,所以我只是使用@Pshemo建議的兩個測試,它的工作 – Selphiron