如何将字符串拆分为文本和数字? [英] How to split strings into text and number?

查看:56
本文介绍了如何将字符串拆分为文本和数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想像这样拆分字符串

'foofo21'
'bar432'
'foobar12345'

进入

['foofo', '21']
['bar', '432']
['foobar', '12345']

有人知道在 python 中执行此操作的简单方法吗?

Does somebody know an easy and simple way to do this in python?

推荐答案

我会通过以下方式使用 re.match 来解决这个问题:

I would approach this by using re.match in the following way:

import re
match = re.match(r"([a-z]+)([0-9]+)", 'foofo21', re.I)
if match:
    items = match.groups()
print(items)
>> ("foofo", "21")

这篇关于如何将字符串拆分为文本和数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆