用于匹配未被引号括起的逗号的正则表达式

Ray*_*yne 5 java regex clojure

我正在使用Clojure,所以这是在Java正则表达式的上下文中.

这是一个示例字符串:

{:a "ab,cd, efg", :b "ab,def, egf,", :c "Conjecture"}
Run Code Online (Sandbox Code Playgroud)

重要的位是每个字符串后面的逗号.我希望能够用Java的replaceAll方法用换行符替换它们.正则表达式将匹配任何未被引号括起的逗号.

如果我没遇好,请问,我会很乐意澄清任何事情.

编辑:对不起标题中的混淆.我很久没醒了.

字符串:{:a "ab, cd efg",}< - 在此示例中,末尾的逗号将匹配,但引号内的逗号不匹配.

字符串:{:a 3, :b 3,}< - 每个逗号都匹配.

String {:a "abcd,efg" :b "abcedg,e"}< - 每个逗号都不匹配.

Bar*_*ers 19

正则表达式:

,\s*(?=([^"]*"[^"]*")*[^"]*$)
Run Code Online (Sandbox Code Playgroud)

火柴:

{:a "ab,cd, efg", :b "ab,def, egf,", :c "Conjecture"}
                ^                  ^
                ^                  ^
Run Code Online (Sandbox Code Playgroud)

和:

{:a "ab, cd efg",}
                ^
                ^
Run Code Online (Sandbox Code Playgroud)

与逗号不匹配:

{:a "abcd,efg" :b "abcedg,e"}
Run Code Online (Sandbox Code Playgroud)

但是当出现转义引号时,就像这样:

{:a "ab,\" cd efg",} // only the last comma should match
Run Code Online (Sandbox Code Playgroud)

然后正则表达式解决方案将无法正常工作.

正则表达式的简要说明:

,            # 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
Run Code Online (Sandbox Code Playgroud)

换句话说:匹配任何前面有零或者偶数引号的逗号(直到字符串结尾).