【问题标题】:Regex until character but if not preceded by another character正则表达式直到字符,但如果前面没有另一个字符
【发布时间】:2019-01-21 16:19:11
【问题描述】:

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

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

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

有什么想法吗?

编辑

使用以下字符串:

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

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

【问题讨论】:

    标签: python regex lookahead lookbehind


    【解决方案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.
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 好吧,至少在 Debuggex 中不能正确停止 -> 例如,此字符串:Localize("/Windows/Actions/DeleteActionWarning=您要尝试的操作 \"delete\ "在本文档中被引用。") + "想要继续?";我希望它在“文档”之后停止。来了,因为它是第一个没有尾随 \ 出现的 " (出现在 "delete" 周围)
      • 如果有任何帮助,请使用适当的示例编辑问题,谢谢!
      猜你喜欢
      • 2021-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-19
      • 2013-12-18
      • 2014-09-17
      相关资源
      最近更新 更多