-3

How can I extract with python all strings starting with 'a[' and ending with ']'?

for example

str= "a[0]*x**3+a[13]"

result : a[0], a[13]

thanks

Siva Shanmugam
  • 662
  • 9
  • 19

1 Answers1

0

We can use re.findall here:

inp = "a[0]*x**3+a[13]"
matches = re.findall(r'\ba\[.*?\]', inp)
print(matches)  # ['a[0]', 'a[13]']
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360