0

I know how to capture a single instance of a substring between two markers:

Python 3 How to get string between two points using regex?

I tested that approach out with this string:

text = 'blah.blah${capture1}.${capture2}'

I wanted to get all the substrings between these markers '${' and '}', but it only gets the first one.

>>> text = 'blah.blah${capture1}.${capture2}'
>>> found = re.search('\$\{(.+?)\}', text)
>>> found.groups()
('capture1',)
>>> len(found.groups())
1
>>> 

How do I get all of them?

martineau
  • 119,623
  • 25
  • 170
  • 301
Selena
  • 2,208
  • 8
  • 29
  • 49

1 Answers1

1

You need a regex method that will find all the matches in a string. You should try re.findall('\$\{(.+?)\}', text) or re.finditer('\$\{(.+?)\}', text). The first will return a list, the second will return an iterable.

ldtcoop
  • 680
  • 4
  • 14