Python - 使用正则表达式将数字和字母拆分为子字符串 [英] Python - Splitting numbers and letters into sub-strings with regular expression

查看:87
本文介绍了Python - 使用正则表达式将数字和字母拆分为子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个公制测量转换器.用户需要输入一个表达式,例如 125km(一个数字后跟一个单位缩写).要进行转换,必须将数值从缩写中拆分出来,从而产生诸如 [125, 'km'] 之类的结果.我使用正则表达式 re.split 完成了此操作,但是它会在结果列表中生成不需要的项目:

导入重新s = '125km'打印(re.split('(\d+)', s))

输出:

['', '125', 'km']

我不需要也不想要开头的''.如何使用正则表达式简单地将字符串的数字部分与字母部分分开以生成列表?

解决方案

re.findall 有什么问题?

<预><代码>>>>s = '125km'>>>re.findall(r'[A-Za-z]+|\d+', s)['125','公里']

[A-Za-z]+ 匹配一个或多个字母.|\d+ 一位或多位数字.

使用列表理解.

<预><代码>>>>[i for i in re.split(r'([A-Za-z]+)', s) if i]['125','公里']>>>[i for i in re.split(r'(\d+)', s) if i]['125','公里']

I am creating a metric measurement converter. The user is expected to enter in an expression such as 125km (a number followed by a unit abbreviation). For conversion, the numerical value must be split from the abbreviation, producing a result such as [125, 'km']. I have done this with a regular expression, re.split, however it produces unwanted item in the resulting list:

import re
s = '125km'
print(re.split('(\d+)', s))

Output:

['', '125', 'km']

I do not need nor want the beginning ''. How can I simply separate the numerical part of the string from the alphabetical part to produce a list using a regular expression?

解决方案

What's wrong with re.findall ?

>>> s = '125km'
>>> re.findall(r'[A-Za-z]+|\d+', s)
['125', 'km']

[A-Za-z]+ matches one or more alphabets. | or \d+ one or more digits.

OR

Use list comprehension.

>>> [i for i in re.split(r'([A-Za-z]+)', s) if i]
['125', 'km']
>>> [i for i in re.split(r'(\d+)', s) if i]
['125', 'km']

这篇关于Python - 使用正则表达式将数字和字母拆分为子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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