正则表达式:
,\s*(?=([^"]*"[^"]*")*[^"]*$)
匹配:
{:a "ab,cd, efg", :b "ab,def, egf,", :c "Conjecture"}
^ ^
^ ^
和:
{:a "ab, cd efg",}
^
^
并且不匹配逗号:
{:a "abcd,efg" :b "abcedg,e"}
但是当转义引号会出现时,像这样:
{:a "ab,\" cd efg",} // only the last comma should match
那么正则表达式解决方案将不起作用。
正则表达式的简要说明:
, # match the character ','
\s* # match a whitespace character: [ \t\n\x0B\f\r] and repeat it zero or more times
(?= # start positive look ahead
( # start capture group 1
[^"]* # match any character other than '"' and repeat it zero or more times
" # match the character '"'
[^"]* # match any character other than '"' and repeat it zero or more times
" # match the character '"'
)* # end capture group 1 and repeat it zero or more times
[^"]* # match any character other than '"' and repeat it zero or more times
$ # match the end of the input
) # end positive look ahead
换句话说:匹配前面有零个或偶数个引号的任何逗号(直到字符串的末尾)。