簡體   English   中英

正則表達式直到字符但如果前面沒有另一個字符

[英]Regex until character but if not preceded by another character

我想創建一個正則表達式來匹配一個用Localize("分割的字符串,並且應該在"彈出時結束,而不是在"轉義時結束(前面是\\ )。

我當前的正則表達式沒有考慮到“除非前面有”看起來像:

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

有任何想法嗎 ?

編輯

使用以下字符串:

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

我希望它在document.后停止document. 來了,因為這是第一次"露面沒有尾隨\\ (這說明周圍delete

您可以使用

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

請參閱此正則表達式演示

詳情

  • \\bLocalize - 一個完整的詞Localize
  • \\(" - a ("子串
  • ([^"\\\\]*(?:\\\\.[^"\\\\]*)*) - 捕獲組 1:
    • [^"\\\\]* - 除"\\之外的 0 個或多個字符
    • (?:\\\\.[^"\\\\]*)* - 轉義字符的 0 次或多次重復,后跟除"\\以外的 0 次或更多字符

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

您可以使用非正則表達式運算符 ^

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM