2

I refer to the question how to extract a substring from inside a string in Python? and have further question.

What if my string is something like:

gfgfdAAA1234ZZZsddgAAA4567ZZZuijjk

I want to extract 1234 and 4567, is it stored as a list?

Community
  • 1
  • 1
lokheart
  • 22,255
  • 36
  • 92
  • 166

2 Answers2

4
import re
re.findall('\d+','gfgfdAAA1234ZZZsddgAAA4567ZZZuijjk')
['1234', '4567']
markcial
  • 8,713
  • 4
  • 30
  • 41
1

Let me know if I'm not understanding you correctly, but you can certainly extract these substrings as a list, though you do it slightly differently:

>>> import re
>>> text = 'gfgfdAAA1234ZZZsddgAAA4567ZZZuijjk'
>>> re.findall(r'[0-9]{4}', text)
['1234', '4567']
Slater Victoroff
  • 20,556
  • 18
  • 82
  • 140