简体   繁体   中英

Regex Python, Match anything between A and B unless C

A question about Regex. I have the following patterns that i want to match:

A()()()(B)         MATCH
A()()(B)           MATCH
A()(B)             MATCH
A(B)               MATCH

ALSO if anything is in the brackets apart from the phrase (NO) then that should match also:

A(abc)(B)          MATCH
A()(def)(B)        MATCH
A()()(ghij)(B)     MATCH
A(klmn)(opq)()(B)  MATCH
A(NO)(B)           NO MATCH
A()()(NO)(B)       NO MATCH
A(abc)(NO)(B)      NO MATCH

However I do not want the phrase to match if there is something that is not in the brackets:

Aabc(B)           NO MATCH
A()defg()(B)      NO MATCH

I have the following:

A\(.*\)*(?<!\(NO\))\(B\)

PLEASE HELP!

You can do:

^A(\((?!NO\))[^)]*\))*\(B\)$

Demo

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