如何根据数字/非数字拆分字符串(使用正则表达式?)

rik*_*kit 0 python regex string tokenize

我想将字符串拆分为python中的列表,具体取决于数字/非数字.例如,

5 55+6+  5/
Run Code Online (Sandbox Code Playgroud)

应该回来

['5','55','+','6','+','5','/']
Run Code Online (Sandbox Code Playgroud)

我现在有一些代码循环遍历字符串中的字符并使用re.match("\ d")或("\ D")测试它们.我想知道是否有更好的方法来做到这一点.

PS:必须与python 2.4兼容

ken*_*ytm 5

假设+6到5之间需要匹配(你错过了),

>>> import re
>>> s = '5 55+6+ 5/'
>>> re.findall(r'\d+|[^\d\s]+', s)
['5', '55', '+', '6', '+', '5', '/']
Run Code Online (Sandbox Code Playgroud)