正则表达式直到字符但如果前面没有另一个字符

Regex until character but if not preceded by another character

我想创建一个正则表达式来匹配与 Localize(" 共享的字符串,并且应该在 " 弹出时结束,而不是在 " 被转义时结束(前面是 \).

我当前的正则表达式没有考虑到 "unless preceded by" 看起来像:

\bLocalize\(\"(.+?)(?=\")

有什么想法吗?

编辑

使用以下字符串:

Localize("/Windows/Actions/DeleteActionWarning=The action you are trying to \"delete\" is referenced in this document.") + " Want to Proceed ?";

我希望它在 document. 出现后停止,因为它是第一个没有尾随 \"(出现在 delete 左右)

您可以使用非正则表达式运算符 ^

\bLocalize(\".*?[^\]\"

您可以使用

\bLocalize\("([^"\]*(?:\.[^"\]*)*)

参见 this regex demo

详情:

  • \bLocalize - 一个完整的单词 Localize
  • \(" - (" 子串
  • ([^"\]*(?:\.[^"\]*)*) - 捕获组 1:
    • [^"\]* - "\
    • 以外的 0 个或更多字符
    • (?:\.[^"\]*)* - 0 次或多次重复的转义字符后跟 0 次或多次 "\
    • 以外的字符

在Python中,用

声明模式
reg = r'\bLocalize\("([^"\]*(?:\.[^"\]*)*)'

Demo:

import re
reg = r'\bLocalize\("([^"\]*(?:\.[^"\]*)*)'
s = "Localize(\"/Windows/Actions/DeleteActionWarning=The action you are trying to \\"delete\\" is referenced in this document.\") + \" Want to Proceed ?\";"
m = re.search(reg, s)
if m:
    print(m.group(1))
# => /Windows/Actions/DeleteActionWarning=The action you are trying to \"delete\" is referenced in this document.