Regex until字符,但如果前面没有另一个ch

2024-10-02 06:25:04 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我当前的正则表达式不考虑“除非前面有”这样的计数,如下所示:

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

有什么想法吗?你知道吗

编辑

使用以下字符串:

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

我希望它在document.到来后停止,因为它是第一个"出现而没有后面的\(出现在delete附近)


Tags: theto字符串youactions编辑windowsaction
2条回答

可以使用not regex操作符^

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

你可以用

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

this regex demo。你知道吗

详细信息:

  • \bLocalize-一个完整的单词Localize
  • \("-a ("子串
  • ([^"\\]*(?:\\.[^"\\]*)*)-捕获组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.

相关问题 更多 >

    热门问题