【问题标题】:Regex look-ahead with non-capturing group not working as intended非捕获组的正则表达式前瞻未按预期工作
【发布时间】:2021-01-14 14:13:18
【问题描述】:

下面有我想从中提取月份的文本(在本例中为 7 月)。 word_pattern 确保文本包含这些词, 而month_pattern 将提取月份。所以首先我验证文本段落 包含某些单词,如果是的话,我会尝试提取month

当模式单独使用时,它们会得到匹配,但如果我尝试将它们组合起来 我最终没有匹配。 我不知道我做错了什么。

import re

text = ''' The number of shares of the
registrant’s common stock outstanding as
of July 31, 2017 was 52,833,429.'''

# patterns
word_pattern = r'(?=.*outstanding[.,]?)(?=.*common)(?=.*shares)'

month_pattern = r'(Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Oct(?:ober)?|(Nov|Dec)(?:ember)?)'


pattern = word_pattern + month_pattern

print(re.search(pattern, text, flags = re.IGNORECASE|re.DOTALL))

预期结果:

【问题讨论】:

    标签: python regex regex-lookarounds


    【解决方案1】:

    Regex 不能像那样轻易连接。问题是您的单词模式仅使用前瞻,因此不会将位置向前移动,当月份仅显示在字符串中间时,这会成为一个问题。因此,您需要使用弥补差距的量词让光标前进到月份位置,例如.*试试

    (?=.*outstanding[.,]?)(?=.*common)(?=.*shares).*(Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Oct(?:ober)?|(Nov|Dec)(?:ember)?)
    

    Demo

    或者pattern = word_pattern +'.*'+ month_pattern 应该可以解决问题。

    可以在捕获组 1 中找到结果:re.search(...).group(1)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-25
      • 1970-01-01
      • 2014-04-28
      相关资源
      最近更新 更多