【问题标题】:Is there any way to extract a specfic pattern in a given String using Python Regex [duplicate]有没有办法使用 Python Regex [重复] 提取给定字符串中的特定模式
【发布时间】: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


    【解决方案1】:
    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']
    

    【讨论】:

    • 谢谢@bigbounty。它奏效了
    猜你喜欢
    • 2022-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-17
    • 2022-12-18
    • 1970-01-01
    • 2019-11-01
    • 1970-01-01
    相关资源
    最近更新 更多