1

我想创建一个正则表达式来匹配一个字符串,该字符串与弹出Localize("并应该在 a"弹出时结束,但不是在"转义时(前面是\)。

我当前的正则表达式不考虑“除非前面”看起来像:

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

有任何想法吗 ?

编辑

使用以下字符串:

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

我希望它在来之后停止document.,因为它是第一个"出现而没有尾随的\(出现在 周围delete

4

2 回答 2

1

您可以使用

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

请参阅此正则表达式演示

详情

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

在 Python 中,使用

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

演示

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.
于 2019-01-21T16:42:12.950 回答
0

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

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

于 2019-01-21T16:30:21.943 回答