Python - re.findall 返回不需要的结果 [英] Python - re.findall returns unwanted result

查看:60
本文介绍了Python - re.findall 返回不需要的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

re.findall("(100|[0-9][0-9]|[0-9])%", "89%")

这仅返回结果 [89] 而我需要返回整个 89%.请问有什么办法吗?

解决方案

琐碎的解决方案:

<预><代码>>>>re.findall("(100%|[0-9][0-9]%|[0-9]%)","89%")['89%']

更漂亮的解决方案:

<预><代码>>>>re.findall("(100%|[0-9]{1,2}%)","89%")['89%']

最漂亮的解决方案:

<预><代码>>>>re.findall("(?:100|[0-9]{1,2})%","89%")['89%']

re.findall("(100|[0-9][0-9]|[0-9])%", "89%")

This returns only result [89] and I need to return the whole 89%. Any ideas how to do it please?

解决方案

The trivial solution:

>>> re.findall("(100%|[0-9][0-9]%|[0-9]%)","89%")
['89%']

More beautiful solution:

>>> re.findall("(100%|[0-9]{1,2}%)","89%")
['89%']

The prettiest solution:

>>> re.findall("(?:100|[0-9]{1,2})%","89%")
['89%']

这篇关于Python - re.findall 返回不需要的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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