【问题标题】:Re-write regex expression number filtering Python重写正则表达式数字过滤Python
【发布时间】:2022-01-17 18:13:41
【问题描述】:

给定一个像 'hello 0796XXXXXX 这样的字符串。今天是你的幸运日£500 Cash',我使用以下正则表达式

re.findall(r"(\b07\d*|\b08\d*|\b09\d*)", t)接收以07开头的数字| 08 | 09 后跟 0 个或多个数字。 ['0796'] 是结果。

如何重新编写代码以使\b\d* 不重复?我试过re.findall(r"\b(07|08|09)\d*)", t),但不幸的是它不起作用,只返回[07]

谢谢

【问题讨论】:

    标签: python regex string filter expression


    【解决方案1】:

    避免括号,把重复的0放在一边:

    re.findall(r"\b0[789]\d*", t)
    

    【讨论】:

      【解决方案2】:
      regex = r"(07|08|09)\d*"
      re.findall(regex, text)
      

      【讨论】:

        猜你喜欢
        • 2016-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-13
        • 2013-09-23
        • 2023-03-29
        相关资源
        最近更新 更多