【问题标题】:Extract number before string using regex [duplicate]使用正则表达式提取字符串前的数字[重复]
【发布时间】:2020-10-09 07:42:09
【问题描述】:

给定:

s = '2X-Large/3X-Large (4 Count)'

有人可以建议如何在 Python 中使用正则表达式从 s 中提取 4 吗?

【问题讨论】:

    标签: python python-3.x regex


    【解决方案1】:

    您可以在这里使用re.findall

    s = "2X-Large/3X-Large (4 Count)"
    counts = re.findall(r'\((\d+) Count\)', s)
    print(counts)
    

    打印出来:

    ['4']
    

    【讨论】:

      猜你喜欢
      • 2022-11-03
      • 1970-01-01
      • 2011-12-05
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 2016-01-02
      • 2016-05-29
      相关资源
      最近更新 更多