Reputation: 323
I am trying to come up with a regular expression that solves the following problem: Of the below string, I only want to match (e):
$(a(b)(c)d)(e)
Of the below string, I only want to match (e(f)):
$(a(b)(c)d)(e(f))
The expression I came up with will only exclude the first inner bracket:
(?<!\$[^)]*)\(.+?\)
Here's the interactive example: regexr.com/5n5c1
I use regular expressions in Python 3 with the regex library.
Upvotes: 1
Views: 132
Reputation: 626950
Install PyPi regex module (pip install regex
) and then you will be able to use
import regex
strs = ['$(a(b)(c)d)(e)', '$(a(b)(c)d)(e(f))']
rx = r'\$(\((?:[^()]++|(?1))*\))(*SKIP)(*F)|(\((?:[^()]++|(?2))*\))'
for s in strs:
print( [x.group() for x in regex.finditer(rx, s)] )
See the Python demo.
Details
\$(\((?:[^()]++|(?1))*\))(*SKIP)(*F)
- a $
char and then a substring between the balanced nested round brackets, and then the (*SKIP)(*F)
verbs make the current match fail and skip it, starting the next pattern search from the place where the skip occurred|
- or(\((?:[^()]++|(?2))*\))
- a substring between the balanced nested round parentheses.See the regex demo.
Upvotes: 1