- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用正则表达式来匹配文本中的圣经经文引用。当前的正则表达式是
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)
这很好地匹配了以下表达式:
"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() 返回所有匹配项,即使它们重叠,或者有没有办法改进我的正则表达式,使其正确匹配最后一位?
谢谢。
最佳答案
消耗的字符被消耗了,你不应该要求正则表达式引擎返回。
从你的例子来看,诗句部分(例如 :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)
(如果您要编写这样一个巨大的 RegEx,请使用 /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')]
您可以将这个想法扩展到您的 RegEx。
关于python - 在 Python 中使用 finditer() 进行重叠匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3027718/
我正在使用 skimage.feature.greycomatrix 来计算图像的 GLCM(灰度共现矩阵),但是当我使用时: from skimage.feature import greycoma
假设我有一个字符串 abbb。我正在尝试使用正则表达式打印以下模式: a ab abb abbb 我尝试这样做: import re line = "abbb" m = re.finditer('ab
例如,我有以下字符串。 input = "a b c d e f 1 2 3 4" 我有以下正则表达式代码。 input = re.sub(r"([0-9])\s+([0-9])", "\1\2",
我想获取span中的元组和macht的str,但我不知道如何访问这个对象,文档中的方法不起作用 import re s = 'sd(asdf(xf))fg' if re.findall('\([A-z
我制作了此模式来获取博客文章的 url 链接(可以在我的网站 url 中用连字符或下划线等分隔,以将其与数据库匹配并显示相应的帖子)。每当我将匹配项附加到列表中时,它们都是重新匹配对象。如何获取匹配的
一个。下面的代码是否会导致 断言错误? 如果是这样,我该如何预防? def finditer_test(pattern, string, flags=0): for match_object
我正在尝试从 Instagram API 获取电子邮件、whatzapp、viber。我将下载的数据存储到一个名为 text 的变量中,并检查电子邮件、watzapp、viber 号码等的模式。 示例
我有一个有点复杂的正则表达式,我试图将其与一个长字符串(65,535 个字符)进行匹配。我正在寻找字符串中多次出现的 re,因此我使用了 finditer。它有效,但由于某种原因它在识别前几次出现后挂
有没有办法将 re.findall 或更好的 re.finditer 功能应用于流(即打开文件句柄以供读取)? 请注意,我不是假设要匹配的模式完全包含在一行输入中(即允许多行模式)。我也不假设最大匹配
我在使用 python 中的 re.finditer() 方法时遇到了一些问题。例如: >>>sequence = 'atgaggagccccaagcttactcgatttaacgcccgcagcct
这个问题在这里已经有了答案: Why can't I iterate twice over the same iterator? How can I "reset" the iterator or
这个问题在这里已经有了答案: Why can't I iterate twice over the same iterator? How can I "reset" the iterator or
我一直在用 python 制作一个简单的着色器。它使用 re.finditer 查找引号之间的所有单词的索引,并在 tkinter 文本框中为这些单词着色。由于某种原因,当盒子打开时,并没有找到所有的
我的字符串是文字记录,我想捕获说话者,特别是他们的第二个名字(仅在完全大写时才需要匹配)此外,我想匹配他们的演讲,直到下一个演讲者开始,我想最终在一个巨大的文本文件上循环这个过程。 问题是匹配仅返回一
我的任务是将“源”文本转换为元素序列:TEXT 和 INPUT。 INPUT 是包裹在两个星号中的部分,而 TEXT 是其他所有内容。 这是一个例子: >>> source = 'I came *ac
我想知道如何通过索引导航由 finditer 正则表达式操作生成的对象。 我的字符串是s = "fish oil X22 stack peanut C4" 这是我的代码: import re word
如果 findall() 足够好,使用 finditer() 有什么好处?findall() 返回所有匹配项,而 finditer() 返回不能像静态列表那样直接处理的匹配对象。 例如: import
有没有办法像使用 findall 那样将不同 finditer 的结果相加?例如: matches = re.finditer(pattern_1, text) + re.finditer(patte
我有以下字符串: '3 4 4 5 5 5 2 2' 我需要像这样从中提取所有连续出现的事件: '44 555 22' 为此,我使用了以下代码。它工作正常: n = input().replace('
我正在使用 Python(实际上是 pl/python)在一个非常大的文本对象中连续查找一系列正则表达式匹配项。这工作正常!每次匹配都是不同的结果,每次替换都会是不同的结果,最终基于循环内的查询。 目
我是一名优秀的程序员,十分优秀!