Python 中与 finditer() 的重叠匹配

发布于 2024-09-05 21:51:33 字数 2834 浏览 1 评论 0原文

我正在使用正则表达式来匹配文本中的圣经经文引用。当前的正则表达式是

REF_REGEX = re.compile('''
  (?<!\w)                        # Not preceded by any words
  (?P<quote>q(?:uote)?\s+)?      # Match optional 'q' or 'quote' followed by many spaces
  (?P<book>                           
    (?:(?:[1-3]|I{1,3})\s*)?     # Match an optional arabic or roman number between 1 and 3.
    [A-Za-z]+                    # Match any alphabetics
  )\.?                           # Followed by an optional dot
  (?:                         
    \s*(?P<chapter>\d+)          # Match the chapter number
    (?:
      [:\.](?P<startverse>\d+)   # Match the starting verse number, preceded by ':' or '.'
        (?:-(?P<endverse>\d+))?  # Match the optional ending verse number, preceded by '-'
    )?                           # Verse numbers are optional
  )
  (?:
    \s+(?:                       # Here be spaces
      (?:from\s+)|(?:in\s+)|(?P<lbrace>\())   # Match 'from[:space:]', 'in[:space:]' or '('
      \s*(?P<version>\w+)        # Match a word preceded by optional spaces
      (?(lbrace)\))              # Close the '(' if found earlier
  )?                             # The whole 'in|from|()' is optional
  ''', re.IGNORECASE | re.VERBOSE | re.UNICODE)

This 与以下表达式很好匹配:

"jn 3:16":                           (None, 'jn', '3', '16', None, None, None),
"matt. 18:21-22":                    (None, 'matt', '18', '21', '22', None, None),
"q matt. 18:21-22":                  ('q ', 'matt', '18', '21', '22', None, None),
"QuOTe jn 3:16":                     ('QuOTe ', 'jn', '3', '16', None, None, None),
"q 1co13:1":                         ('q ', '1co', '13', '1', None, None, None), 
"q 1 co 13:1":                       ('q ', '1 co', '13', '1', None, None, None),
"quote 1 co 13:1":                   ('quote ', '1 co', '13', '1', None, None, None),
"quote 1co13:1":                     ('quote ', '1co', '13', '1', None, None, None),
"jean 3:18 (PDV)":                   (None, 'jean', '3', '18', None, '(', 'PDV'),
"quote malachie 1.1-2 fRom Colombe": ('quote ', 'malachie', '1', '1', '2', None, 'Colombe'),
"quote malachie 1.1-2 In Colombe":   ('quote ', 'malachie', '1', '1', '2', None, 'Colombe'),
"cinq jn 3:16 (test)":               (None, 'jn', '3', '16', None, '(', 'test'),
"Q   IIKings5.13-58   from   wolof": ('Q     ', 'IIKings', '5', '13', '58', None, 'wolof'),
"This text is about lv5.4-6 in KJV only": (None, 'lv', '5', '4', '6', None, 'KJV'),

但无法解析:

"Found in 2 Cor. 5:18-21 ( Ministers":                    (None, '2 Cor', '5', '18', '21', None, None),

因为它返回 (None, 'in', '2', None, None, None, None)

有没有办法让 finditer() 返回所有匹配项,即使它们重叠,或者有没有办法改进我的正则表达式,使其正确匹配最后一点?

谢谢。

I'm using a regex to match Bible verse references in a text. The current regex is

REF_REGEX = re.compile('''
  (?<!\w)                        # Not preceded by any words
  (?P<quote>q(?:uote)?\s+)?      # Match optional 'q' or 'quote' followed by many spaces
  (?P<book>                           
    (?:(?:[1-3]|I{1,3})\s*)?     # Match an optional arabic or roman number between 1 and 3.
    [A-Za-z]+                    # Match any alphabetics
  )\.?                           # Followed by an optional dot
  (?:                         
    \s*(?P<chapter>\d+)          # Match the chapter number
    (?:
      [:\.](?P<startverse>\d+)   # Match the starting verse number, preceded by ':' or '.'
        (?:-(?P<endverse>\d+))?  # Match the optional ending verse number, preceded by '-'
    )?                           # Verse numbers are optional
  )
  (?:
    \s+(?:                       # Here be spaces
      (?:from\s+)|(?:in\s+)|(?P<lbrace>\())   # Match 'from[:space:]', 'in[:space:]' or '('
      \s*(?P<version>\w+)        # Match a word preceded by optional spaces
      (?(lbrace)\))              # Close the '(' if found earlier
  )?                             # The whole 'in|from|()' is optional
  ''', re.IGNORECASE | re.VERBOSE | re.UNICODE)

This matches the following expressions fine:

"jn 3:16":                           (None, 'jn', '3', '16', None, None, None),
"matt. 18:21-22":                    (None, 'matt', '18', '21', '22', None, None),
"q matt. 18:21-22":                  ('q ', 'matt', '18', '21', '22', None, None),
"QuOTe jn 3:16":                     ('QuOTe ', 'jn', '3', '16', None, None, None),
"q 1co13:1":                         ('q ', '1co', '13', '1', None, None, None), 
"q 1 co 13:1":                       ('q ', '1 co', '13', '1', None, None, None),
"quote 1 co 13:1":                   ('quote ', '1 co', '13', '1', None, None, None),
"quote 1co13:1":                     ('quote ', '1co', '13', '1', None, None, None),
"jean 3:18 (PDV)":                   (None, 'jean', '3', '18', None, '(', 'PDV'),
"quote malachie 1.1-2 fRom Colombe": ('quote ', 'malachie', '1', '1', '2', None, 'Colombe'),
"quote malachie 1.1-2 In Colombe":   ('quote ', 'malachie', '1', '1', '2', None, 'Colombe'),
"cinq jn 3:16 (test)":               (None, 'jn', '3', '16', None, '(', 'test'),
"Q   IIKings5.13-58   from   wolof": ('Q     ', 'IIKings', '5', '13', '58', None, 'wolof'),
"This text is about lv5.4-6 in KJV only": (None, 'lv', '5', '4', '6', None, 'KJV'),

but it fails to parse:

"Found in 2 Cor. 5:18-21 ( Ministers":                    (None, '2 Cor', '5', '18', '21', None, None),

because it returns (None, 'in', '2', None, None, None, None) instead.

Is there a way to get finditer() to return all matches, even if they overlap, or is there a way to improve my regex so it matches this last bit properly?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁拥 2024-09-12 21:51:33

消耗的字符已被消耗,您不应该要求正则表达式引擎返回。

从您的示例来看,诗句部分(例如 :1)似乎不是可选的。删除它将匹配最后一位。

ref_regex = re.compile('''
(?<!\w)                      # Not preceeded by any words
((?i)q(?:uote)?\s+)?            # Match 'q' or 'quote' followed by many spaces
(
    (?:(?:[1-3]|I{1,3})\s*)?    # Match an arabic or roman number between 1 and 3.
    [A-Za-z]+                   # Match many alphabetics
)\.?                            # Followed by an optional dot
(?:
    \s*(\d+)                    # Match the chapter number
    (?:
        [:.](\d+)               # Match the verse number
        (?:-(\d+))?             # Match the ending verse number
    )                    # <-- no '?' here
)
(?:
    \s+
    (?:
        (?i)(?:from\s+)|        # Match the keyword 'from' or 'in'
        (?:in\s+)|
        (?P<lbrace>\()      # or stuff between (...)
    )\s*(\w+)
    (?(lbrace)\))
)?
''', re.X | re.U)

(如果您要编写像这样的巨大正则表达式,请使用 /x 标志。)


如果您确实需要重叠匹配,则可以使用前瞻。一个简单的例子是

>>> rx = re.compile('(.)(?=(.))')
>>> x = rx.finditer("abcdefgh")
>>> [y.groups() for y in x]
[('a', 'b'), ('b', 'c'), ('c', 'd'), ('d', 'e'), ('e', 'f'), ('f', 'g'), ('g', 'h')]

您可以将这个想法扩展到您的正则表达式。

A character consumed is consumed, you should not ask the regex engine to go back.

From your examples the verse part (e.g. :1) seems not optional. Removing that will match the last bit.

ref_regex = re.compile('''
(?<!\w)                      # Not preceeded by any words
((?i)q(?:uote)?\s+)?            # Match 'q' or 'quote' followed by many spaces
(
    (?:(?:[1-3]|I{1,3})\s*)?    # Match an arabic or roman number between 1 and 3.
    [A-Za-z]+                   # Match many alphabetics
)\.?                            # Followed by an optional dot
(?:
    \s*(\d+)                    # Match the chapter number
    (?:
        [:.](\d+)               # Match the verse number
        (?:-(\d+))?             # Match the ending verse number
    )                    # <-- no '?' here
)
(?:
    \s+
    (?:
        (?i)(?:from\s+)|        # Match the keyword 'from' or 'in'
        (?:in\s+)|
        (?P<lbrace>\()      # or stuff between (...)
    )\s*(\w+)
    (?(lbrace)\))
)?
''', re.X | re.U)

(If you're going to write a gigantic RegEx like this, please use the /x flag.)


If you really need overlapping matches, you could use a lookahead. A simple example is

>>> rx = re.compile('(.)(?=(.))')
>>> x = rx.finditer("abcdefgh")
>>> [y.groups() for y in x]
[('a', 'b'), ('b', 'c'), ('c', 'd'), ('d', 'e'), ('e', 'f'), ('f', 'g'), ('g', 'h')]

You may extend this idea to your RegEx.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文