简体   繁体   中英

Regex until character but if not preceded by another character

I wanted to create a regex to match a string that sharts with Localize(" and should end when a " pops up, but not when " is escaped (preceded by \\ ).

My current regex which doesnt take into acount that "unless preceded by" looks like:

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

Any ideas ?

EDIT

With the following string:

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

I want it to stop after document. comes, because it is the first " to show up without a trailing \\ (which shows up around delete )

You may use

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

See this regex demo .

Details :

  • \\bLocalize - a whole word Localize
  • \\(" - a (" substring
  • ([^"\\\\]*(?:\\\\.[^"\\\\]*)*) - Capturing group 1:
    • [^"\\\\]* - 0 or more chars other than " and \\
    • (?:\\\\.[^"\\\\]*)* - 0 or more repetitions of an escaped char followed with 0 or more chars other than " and \\

In Python, declare the pattern with

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.

You could use the not regex operator ^

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM