1

I have a string like {string}{string}{string} which I'm able to parse by

import re
input_string = '{string}{string}{string}'
for match in re.finditer(r'{([^}]*)}', input_string):
     print str(match.group(0)) #outputs {string}

but I also want to catch strings with an asterick following a curly bracket.

input_string = '{string}{string}*{string}{string}*'

How do I modify the regex so that both {string} and {string}* will be caught?

I tried doing r'{([^}]*)}|*' but doesn't seem to be working.

Also, what if a string contains curly brackets? ex. {string{3}} How do I make sure only the outer {} is caught and not the one's inside?

I Love Python
  • 862
  • 2
  • 13
  • 20

1 Answers1

1

Add \*? at the end of your regex to match an optional *.

import re
input_string = '{string}{string}*{string}{string}*'
for match in re.finditer(r'{([^}]*)}\*?', input_string):
     print str(match.group(0)) #outputs {string}

Output:

{string}
{string}*
{string}
{string}*

As for nested brackets, simple regular expressions cannot parse that. You can use recursive regular expressions for this, but it becomes unreadable (example). You should use a proper parsing library, more discussion and examples here.

Community
  • 1
  • 1
Dogbert
  • 212,659
  • 41
  • 396
  • 397