正则表达式,直到字符,但如果没有其他字符

时间:2019-01-21 16:19:12

标签: python regex lookahead lookbehind

我想创建一个正则表达式来匹配以Localize("为结尾的字符串,并且应在弹出"时结束,而不是在"被转义时结束(以{{1开头) }}。

我当前的正则表达式没有考虑到“除非前面”看起来像这样:

\

有什么想法吗?

编辑

带有以下字符串:

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

我希望它在Localize("/Windows/Actions/DeleteActionWarning=The action you are trying to \"delete\" is referenced in this document.") + " Want to Proceed ?"; 到来后停止,因为它是第一个出现而没有尾随document.(在"附近出现)的\

2 个答案:

答案 0 :(得分:1)

您可以使用

\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.

答案 1 :(得分:0)

您可以使用not regex运算符^

  

\ bLocalize(\“。*?[^ \] \”