Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 正则表达式,包括具有相同开头的重叠匹配_Python_Regex_Python 3.x - Fatal编程技术网

Python 正则表达式,包括具有相同开头的重叠匹配

Python 正则表达式,包括具有相同开头的重叠匹配,python,regex,python-3.x,Python,Regex,Python 3.x,我正在使用Python 3.6 我的目标是匹配一个正则表达式,它可以匹配多个字符串,重叠和/或从同一位置开始,例如: re.findall('B.*A','BADACBA') 其中: ['BADACBA'] 但我想: ['BADACBA','BADA','BA','BA'] (第二个'BA'是因为字符串中有两个'BA'实例) 根据的建议,我尝试将其包含在前瞻中: re.findall(“(?=(B.*A))”,“BADACBA”) 其中: ['BADACBA','BA'] 哪一个更好,

我正在使用Python 3.6

我的目标是匹配一个正则表达式,它可以匹配多个字符串,重叠和/或从同一位置开始,例如:

re.findall('B.*A','BADACBA')
其中:

['BADACBA']
但我想:

['BADACBA','BADA','BA','BA']
(第二个
'BA'
是因为字符串中有两个
'BA'
实例)

根据的建议,我尝试将其包含在前瞻中:

re.findall(“(?=(B.*A))”,“BADACBA”)
其中:

['BADACBA','BA']
哪一个更好,但仍然不是我想要的

我还尝试了
regex
模块:

regex.findall('B.*A','BADACBA',overlapped=True)
但它仍然返回:

['BADACBA','BA']
我无法找到可以找到所有匹配项的内容。因为我有很多这样的正则表达式,硬编码的解决方案不会有多大帮助。是否有这样的模块/功能


谢谢

正如我上面所说,regex主要是一种线性的、单一规则的引擎——您可以在贪婪捕获与否之间进行选择,但不能同时选择两者。此外,大多数regex引擎不支持重叠匹配(甚至那些支持重叠匹配的人也用子字符串/强制头部移动来伪造重叠匹配),因为它也不符合regex的理念

如果只查找两个子字符串之间的简单重叠匹配,可以自己实现:

def find_substrings(data, start, end):
    result = []
    s_len = len(start)  # a shortcut for `start` length
    e_len = len(end)  # a shortcut for `end` length
    current_pos = data.find(start)  # find the first occurrence of `start`
    while current_pos != -1:  # loop while we can find `start` in our data
        # find the first occurrence of `end` after the current occurrence of `start`
        end_pos = data.find(end, current_pos + s_len)
        while end_pos != -1:  # loop while we can find `end` after the current `start`
            end_pos += e_len  # just so we include the selected substring
            result.append(data[current_pos:end_pos])  # add the current substring
            end_pos = data.find(end, end_pos)  # find the next `end` after the curr. `start`
        current_pos = data.find(start, current_pos + s_len)  # find the next `start`
    return result
这将产生:

substrings = find_substrings("BADACBA", "B", "A")
# ['BA', 'BADA', 'BADACBA', 'BA']

但是您必须修改它以进行更复杂的匹配。

您的模式
B.*A
是贪婪的,因此当它有机会使用整个字符串时,它当然不会捕获BADA。在正则表达式范例下,您所要求的是不符合逻辑的-您最好编写自己的引擎来查找两个子字符串之间所有可能的匹配项。@zwer即使如此,如果我这样做
B.*A
它仍然只匹配两个
BA
是的,因为这样它就不贪婪,所以当它可以只消耗BA时,它不会去追求BADA。。。正如我所说,你试图实现的是与regex哲学无关的。当然,您可以编写特定的模式来捕获特定的字符串,但是在这种情况下,正则表达式并不是真正适合作为通用的解决方案。谢谢,不幸的是,我希望匹配字符串,如
^.{0,6}a.{6}a.{6}[^a]
。哦,好吧,除非有其他事情发生,否则我将不得不重新使用非正则表达式命令。@boboquack-很遗憾,你将无法使用正则表达式。也许您可以创建上面的混合,使用regex查找字符串,然后将其移动到找到的索引+1,依此类推。。。但它仍然无法解决贪婪与非贪婪的匹配。