3

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
  • 23,743
  • 39
  • 98
  • 169

2 Answers2

5
import re
re.findall('\d+','gfgfdAAA1234ZZZsddgAAA4567ZZZuijjk')
['1234', '4567']
markcial
  • 9,041
  • 4
  • 31
  • 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
  • 21,376
  • 21
  • 85
  • 144