0

suppose I have a string containing substrings

# the substrings and the whole string surrounded by parenthesis
string = '((substring1)(substring2))'


I want to get both substring1 & substring2 using regex, but I'm having a problem :(

This is what I have now:

match = re.search('(\(.*\))', string)
print match.groups()


The problem is, the result shows:

('(substring1)(substring2)', )


It seems like the regex matches only the first opening parenthesis and the last closing parenthesis..

In other words, the matching regex is like..

(    match.....    )

instead of

(    (match1)(match2)   )

How do I make the regex catch the INNER parenthesis??

Thanks

user2492270
  • 1,963
  • 6
  • 30
  • 53

1 Answers1

6
>>> re.findall('\([^()]*\)', string)
['(substring1)', '(substring2)']

It is important to note that regexes cannot handle arbitrary levels of nesting. This will work to find the most deeply nested items, but if you're looking for something more sophisticated you're best off abandoning regexes.

John Kugelman
  • 307,513
  • 65
  • 473
  • 519