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 ']
Use the .split()
method instead of .findall()
:
>>> reg.split(s)
['', ' asd 12 ', ' sfd g ', '']
It includes all results in between the matches, including the empty strings at the start and end. You can filter those out:
>>> filter(None, reg.split(s))
[' asd 12 ', ' sfd g ']
although on Python 3 you'd need to use list(filter(None, reg.split(s)))
, or iterate over the result of filter()
.
使用re.split
而不是re.findall
。
You could try something like:
>>> reg = re.compile(r'(?:\d{3})?(.*?)\d{3}')
>>> reg.findall("123 asd 12 456 sfd g 789")
[' asd 12 ', ' sfd g ']
Since .findall()
won't find overlapping matches, you need to specify the first group of numbers as being an optional match. In the end, it might be better to take a different approach than regexes alone for a more robust solution.
>>> s = "123 asd 12 456 sfd g 789"
>>> filter(None, re.compile("\d{3}").split(s))
[' asd 12 ', ' sfd g ']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.