我有这样的字符串:
"abcd \"efg \"hi"jklm"
我想获得两个第一个字符之间的子字符串",这不是\"
例如,在上面的字符串中,我想得到abcd\" efg\" hi
当前,我替换\"另一个字符,然后使用正则表达式"([^"]*)"提取两个第一个字符之间的子字符串".有没有办法直接使用正则表达式而不用\"另一个字符替换.
使用这个正则表达式:
[^\\]?"(.*?[^\\])"
Run Code Online (Sandbox Code Playgroud)
说明:
[^\\]? match an optional single character which is not backslash
"(.*? match a quote followed by anything (non-greedy)
[^\\])" match a quote preceded by anything other than backslash
Run Code Online (Sandbox Code Playgroud)
此正则表达式将匹配开头报价和没有反斜杠的结束报价之间的最小内容.