Quantcast
Channel: Python regex to find nested parentheses outside double quotes - Stack Overflow
Viewing all articles
Browse latest Browse all 3

Answer by Jan for Python regex to find nested parentheses outside double quotes

$
0
0

Using regex instead of re, you could go with

"[^"]+"(*SKIP)(*FAIL) # ignore anything between double quotes|                     # or\(    (?:[^()]*|(?R))+  # match nested parentheses\)

See a demo on regex101.com.


In Python this could be
import regex as redata = """"Hello World (Don't want to strip this (also not this))"  anything outside round brackets should remain as is(strip this (strip this also as it is outside double quotes))"""rx = re.compile(r'''"[^"]+"(*SKIP)(*FAIL)    |    \(        (?:[^()]*|(?R))+    \)''', re.VERBOSE)data = rx.sub("", data)print(data)

Yielding

"Hello World (Don't want to strip this (also not this))"  anything outside round brackets should remain as is

Viewing all articles
Browse latest Browse all 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>