How do I find all the strings between a regex pattern?
For example,
>>> s="123 asd 12 456 sfd g 789"
>>> reg=re.compile("\d{3}")
>>> reg.findall(s)
['123', '456', '789']
I want to find:
[' asd 12 ', ' sfd g ']
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use the
.split()method instead of.findall():It includes all results in between the matches, including the empty strings at the start and end. You can filter those out:
although on Python 3 you’d need to use
list(filter(None, reg.split(s))), or iterate over the result offilter().