Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.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/4/regex/16.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_Bioinformatics - Fatal编程技术网

Python 无法获取正则表达式以捕获最后一个组

Python 无法获取正则表达式以捕获最后一个组,python,regex,bioinformatics,Python,Regex,Bioinformatics,我试图用python编写一个正则表达式来解析一个Newick树,但就我而言,我无法让它的最后一部分匹配。我需要解析三种类型的Newick格式: ((A,B),C); ((A:0.1,B:0.2),C:0.3); ((A:[c1]0.1,B:[c2]0.2),C:[c2]0.3); …每个都包含三个标签(A、B、C)和各种其他信息位。我想要三个标签。这是我的正则表达式: regex = re.compile(r""" ( ([,(]) # boundary

我试图用python编写一个正则表达式来解析一个Newick树,但就我而言,我无法让它的最后一部分匹配。我需要解析三种类型的Newick格式:

((A,B),C);
((A:0.1,B:0.2),C:0.3);
((A:[c1]0.1,B:[c2]0.2),C:[c2]0.3);
…每个都包含三个标签(A、B、C)和各种其他信息位。我想要三个标签。这是我的正则表达式:

regex = re.compile(r"""
(
    ([,(])              # boundary
    ([A-Z0-9_\-\.]+)    # label
    (:)?                # optional colon
    (\[.+?\])?          # optional comment chunk
    (\d+\.\d+)?         # optional branchlengths
    ([),])              # end!
)
""", re.IGNORECASE + re.VERBOSE + re.DOTALL)

。。。但是,我只得到A和C。从来没有得到过B。我已经跟踪到了最后捕获的组([),])-如果我删除这个,那么我得到了所有A、B和C。请帮助-这里出了什么问题

如果你只需要标签,你能不能用一个简单的正则表达式,比如
[(,]([a-Z])

结果:

['A', 'B', 'C'] ['A', 'B', 'C'] ['A', 'B', 'C'] ['A','B','C'] ['A','B','C'] ['A','B','C']
问题可能是您正在寻找不重叠的正则表达式实例。 像
findall
这样的方法不会返回B,因为A的匹配项消耗了
B
之前

>>> regex.findall("((A:[c1]0.1,B:[c2]0.2),C:[c2]0.3);")
[('(A:[c1]0.1,', '(', 'A', ':', '[c1]', '0.1', ','), (',C:[c2]0.3)', ',', 'C', ':', '[c2]', '0.3', ')')]
将结束模式更改为向前看(这样它就不会消耗任何东西)解决了这个问题

>>> regex = re.compile(r"""
... (
...     ([,(])              # boundary
...     ([A-Z0-9_\-\.]+)    # label
...     (:)?                # optional colon
...     (\[.+?\])?          # optional comment chunk
...     (\d+\.\d+)?         # optional branchlengths
...     (?=[),])            # end!
... )
... """, re.IGNORECASE + re.VERBOSE + re.DOTALL)
>>>
>>> regex.findall("((A:[c1]0.1,B:[c2]0.2),C:[c2]0.3);")
[('(A:[c1]0.1', '(', 'A', ':', '[c1]', '0.1'), (',B:[c2]0.2', ',', 'B', ':', '[c2]', '0.2'), (',C:[c2]0.3', ',', 'C', ':
', '[c2]', '0.3')]
>>>
否则,您可以使用
search
迭代并使用
pos
参数进行monkey操作,而不是使用
findall

大概是这样的:

>>> x = "((A:[c1]0.1,B:[c2]0.2),C:[c2]0.3);"
>>> r = []
>>> index = 0
>>> while True:
...     m = regex.search(x, index)
...     if not m:
...        break
...     r.append(m.groups())
...     index = m.end(7)-1
...
>>> r
[('(A:[c1]0.1,', '(', 'A', ':', '[c1]', '0.1', ','), (',B:[c2]0.2)', ',', 'B', ':', '[c2]', '0.2', ')'), (',C:[c2]0.3)',
 ',', 'C', ':', '[c2]', '0.3', ')')]
我希望得到整个块(从开始边界到结束边界),同时将其解析为那些子部分(开始、标签、注释、分支长度、结束)。
>>> x = "((A:[c1]0.1,B:[c2]0.2),C:[c2]0.3);"
>>> r = []
>>> index = 0
>>> while True:
...     m = regex.search(x, index)
...     if not m:
...        break
...     r.append(m.groups())
...     index = m.end(7)-1
...
>>> r
[('(A:[c1]0.1,', '(', 'A', ':', '[c1]', '0.1', ','), (',B:[c2]0.2)', ',', 'B', ':', '[c2]', '0.2', ')'), (',C:[c2]0.3)',
 ',', 'C', ':', '[c2]', '0.3', ')')]