简体   繁体   中英

Python regex to replace everything between a string and a period followed by a parentheses

txt = **(Note:** %Program Files% is the default Program Files folder, usually C:\Program Files in Windows 2000, Server 2003, and XP (32-bit), Vista (32-bit), 7 (32-bit), and 8 (32-bit), or C:\Program Files (x86) in Windows XP (64-bit), Vista (64-bit), 7 (64-bit), and 8 (64-bit) **.)** ***IMPORTANT TEXT*** **(Note:** %Application Data% is the current user's Application Data folder, which is usually C:\Documents and Settings\{user name}\Application Data on Windows 2000, XP, and Server 2003, or C:\Users\{user name}\AppData\Roaming on Windows Vista, 7, and 8.. %Program Files% is the default Program Files folder, usually C:\Program Files in Windows 2000, Server 2003, and XP (32-bit), Vista (32-bit), 7 (32-bit), and 8 (32-bit), or C:\Program Files (x86) in Windows XP (64-bit), Vista (64-bit), 7 (64-bit), and 8 (64-bit)**.)** ***IMPORTANT TEXT*** **(Note:** %Application Data% is the current user's Application Data folder, which is usually C:\Documents and Settings\{user name}\Application Data on Windows 2000, XP, and Server 2003, or C:\Users\{user name}\AppData\Roaming on Windows Vista, 7, and 8 **.)**

I have a text like txt and I would like to remove everything that is between *(NOTE: * and .) , removing only the ( IMPORTANT TEXT ). I have written this regex \(Note:([^)]+\.\)) but it only finds the last Note. Also, I tried the \(Note:([^)]+.\)) but that also consider the first closing parentheses. I am using re.sub(\(Note:([^)]+.\)), '', txt) Any idea?

Honestly this is really messy and you can probably optimize this but if the pattern is consistent this should work

**(Note:**(.*?)**.)**

Modifying yours just a tiny bit, this also works:

\(Note:(.+?\.\))

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