【问题标题】:Python Regex Cannot get expression to matchPython正则表达式无法获得匹配的表达式
【发布时间】:2021-04-10 05:32:46
【问题描述】:

我有一个字符串,我希望从中提取一个值。字符串为:'data - hk = "136 HK"'。我知道,数据 - hk = 总是在值之前,因此我可以使用简单的 python str.split()。这不是一个简洁的解决方案。我想更熟悉正则表达式,但到目前为止我的尝试都失败了。

这是我尝试过的:

        text = 'data - hk = "136 HK"'
        
        # With simple text split - returns 136
        int(text.split("data - hk = ")[1].split('"')[1].split(" HK")[0])

        # With regex - returns nothing
        re.search("[\n\r].*data - hk:\s*([^\n\r]*)", str(text))

有人可以指导我,我需要在正则表达式中更改什么吗?

【问题讨论】:

  • 可能类似于re.findall(r'^data - hk = "(\d+) HK"', text)。但是捕获的子字符串总是整数吗?看起来像是你自己尝试拆分的结果。
  • 谢谢,是的,它永远是整数
  • 我在之前的评论中忘记了结束字符串锚点。 @Rva92。它最终对你有用吗?
  • 是的,非常感谢。将其发布为答案,我会标记它

标签: python-3.x regex


【解决方案1】:

您似乎可以使用re.findall 验证您的字符串并提取您的整数:

re.findall(r'^data - hk = "(\d+) HK"$', text)

在线查看demo

  • ^ - 起始线锚点。
  • data - hk = " - 提到的字符串的文字匹配。
  • (\d+) - 用于检索 1+ 位整数的捕获组。
  • HK" - 提到的字符串的另一个字面匹配。
  • $ - 结束线锚。

【讨论】:

    【解决方案2】:

    使用

    data - hk\D*(\d+(?:\.\d+)?)
    

    proof

    解释

    --------------------------------------------------------------------------------
      data - hk                'data - hk'
    --------------------------------------------------------------------------------
      \D*                      non-digits (all but 0-9) (0 or more times
                               (matching the most amount possible))
    --------------------------------------------------------------------------------
      (                        group and capture to \1:
    --------------------------------------------------------------------------------
        \d+                      digits (0-9) (1 or more times (matching
                                 the most amount possible))
    --------------------------------------------------------------------------------
        (?:                      group, but do not capture (optional
                                 (matching the most amount possible)):
    --------------------------------------------------------------------------------
          \.                       '.'
    --------------------------------------------------------------------------------
          \d+                      digits (0-9) (1 or more times
                                   (matching the most amount possible))
    --------------------------------------------------------------------------------
        )?                       end of grouping
    --------------------------------------------------------------------------------
      )                        end of \1
    

    Code:

    import re
    s = 'data - hk = "136 HK"'
    match = re.search(r'data - hk\D*(\d+(?:\.\d+)?)', s)
    if match is not None:
        print(match.group(1))
    

    结果136.

    【讨论】:

      猜你喜欢
      • 2022-08-17
      • 1970-01-01
      • 2021-09-23
      • 2014-03-30
      • 1970-01-01
      • 2016-07-01
      • 2013-09-22
      • 2016-08-30
      • 2022-12-11
      相关资源
      最近更新 更多