按数字分割字符串

时间:2020-11-06 10:23:38

标签: python regex

我正在尝试按数字将字符串与其他所有内容分开。

string = 'hello_there16.txt'
match = re.match(r"([a-z]+)([0-9]+)",string , re.I)
match.groups()

预期输出:

(hello_there,16,.txt)

但是match的输出为空。

1 个答案:

答案 0 :(得分:0)

如果要拆分,则可以使用re.split

import re
string = 'hello_there16.txt'
result = re.split("([0-9]+)", string)
print(result)

输出:

['hello_there', '16', '.txt']