更新时间:2022-10-18 17:40:08
假设 6 和 5 之间的 +
需要匹配(你错过了),
I want to split a string into a list in python, depending on digit/ not digit. For example,
5 55+6+ 5/
should return
['5','55','+','6','+','5','/']
I have some code at the moment which loops through the characters in a string and tests them using re.match("\d") or ("\D"). I was wondering if there was a better way of doing this.
P.S: must be compatible with python 2.4
Assuming the +
between 6 and 5 needs to be matched (which you're missing),
>>> import re
>>> s = '5 55+6+ 5/'
>>> re.findall(r'\d+|[^\d\s]+', s)
['5', '55', '+', '6', '+', '5', '/']