【发布时间】:2020-11-02 00:07:28
【问题描述】:
如何使用正则表达式获取末尾有 M 的值。
例如
some_str = '/dev/root 298.0M 210.9M 67.3M 76% /'
从上面的字符串中,我想使用 Python 正则表达式精确到 298.0M、210.9M 和 67.M。
【问题讨论】:
标签: python regex python-2.7
如何使用正则表达式获取末尾有 M 的值。
例如
some_str = '/dev/root 298.0M 210.9M 67.3M 76% /'
从上面的字符串中,我想使用 Python 正则表达式精确到 298.0M、210.9M 和 67.M。
【问题讨论】:
标签: python regex python-2.7
import re
pat = '([0-9]+.[0-9]+[M])'
re.findall(pat, "/dev/root 298.0M 210.9M 67.3M 76% /")
输出:
['298.0M', '210.9M', '67.3M']
【讨论】: