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?

1
  • Use re.findall: re.findall('AAA(.+?)ZZZ', text) Commented Feb 11, 2014 at 9:22

2 Answers 2

6
import re
re.findall('\d+','gfgfdAAA1234ZZZsddgAAA4567ZZZuijjk')
['1234', '4567']
Sign up to request clarification or add additional context in comments.

Comments

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']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.