Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.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/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
复杂python正则表达式_Python_Regex_Python 3.x - Fatal编程技术网

复杂python正则表达式

复杂python正则表达式,python,regex,python-3.x,Python,Regex,Python 3.x,我有多行这样的代码: EWSR1{ENST00000397938}:r.1_1364_FLI1{ENST00000429175}:r.1046_3051 EML4{ENST00000318522}:r.1_929_EML4{ENST00000318522}:r.903+188_903+220_ALK{ENST00000389048}:r.4080_6220 FUS{ENST00000254108}:r.1_(608)_FUS{ENST00000254108}:r.(819)_937_DDIT3{

我有多行这样的代码:

EWSR1{ENST00000397938}:r.1_1364_FLI1{ENST00000429175}:r.1046_3051
EML4{ENST00000318522}:r.1_929_EML4{ENST00000318522}:r.903+188_903+220_ALK{ENST00000389048}:r.4080_6220
FUS{ENST00000254108}:r.1_(608)_FUS{ENST00000254108}:r.(819)_937_DDIT3{ENST00000547303}:r.76_872
TCF3{ENST00000262965}:r.1_1795_ins27_PBX1{ENST00000420696}:r.454_6636
EML4{ENST00000318522}:r.?_ALK{ENST00000389048}:r.?
EWSR1{ENST00000397938}:r.1_1364
FLI1{ENST00000429175}:r.1046_3051
EML4{ENST00000318522}:r.1_929
EML4{ENST00000318522}:r.903+188_903+220
ALK{ENST00000389048}:r.4080_6220
FUS{ENST00000254108}:r.1_(608)
FUS{ENST00000254108}:r.(819)_937
DDIT3{ENST00000547303}:r.76_872
TCF3{ENST00000262965}:r.1_1795
ins27
PBX1{ENST00000420696}:r.454_6636
EML4{ENST00000318522}:r.?
ALK{ENST00000389048}:r.?
我需要这样的输出:

对于第一个:

EWSR1 ENST00000397938 1 1364
FLI1 ENST00000429175 1046 3051
第二个:

EML4 ENST00000318522 1 929
EML4 ENST00000318522 903+188 903+220
ALK ENST00000389048 4080 6220
第三个:

FUS ENST00000254108 1 (608)
FUS ENST00000254108 (819) 937
DDIT3 ENST00000547303 76 872
第四个:

TCF3 ENST00000262965 1 1795
ins27
PBX1 ENST00000420696 454 6636
第五个一:

EML4 ENST00000318522 ?
ALK ENST00000389048 ?
我有数百万行代码,所以我想为它设计一个正则表达式,但我无能为力。我创建了一个条件正则表达式,但我相信有一个更优雅、更简单的方法。有人能告诉我一个更简单的方法吗

我现在的解决方案是:

import re
import sys

string = sys.argv[1]
if '+?' in string or '-?' in string or not "?" in string:
    for i in re.findall('\w*?\{.*?\}:r\.[\(\)\?\+\-\d]*_[\(\)\?\+\-\d]*', string):
        if 'ins' in i:
            print(re.findall('ins[A-Za-z0-9]*', i)[0])
            i = re.sub('ins[A-Za-z0-9]*', "", i)
        print(i.lstrip('_').split('{')[0], re.findall('\{(.*?)\}', i.lstrip('_'))[0], " ".join(i.lstrip('_').split('r.')[-1].split('_')))

else:
    for i in re.findall('\w*?\{.*?\}:r\.\?', string):
        if 'ins' in i:
            print(re.findall('ins[A-Za-z0-9]*', i)[0])
            i = re.sub('ins[A-Za-z0-9]*', "", i)
        print(i.lstrip('_').split('{')[0], re.findall('\{(.*?)\}', i.lstrip('_'))[0], " ".join(i.lstrip('_').split('r.')[-1].split('_')))

首先在
(?这里有一个单正则表达式解决方案,它可能不那么优雅,但可以工作:

((?<![^_])ins\d+)_|([a-zA-Z]+[0-9]*)\{([^{}]*)\}:r\.([-()?+\d]+)?(?:_([-()?+\d]+))?
结果:

[
 ['EWSR1', 'ENST00000397938', '1', '1364'],
 ['FLI1', 'ENST00000429175', '1046', '3051'],

 ['EML4', 'ENST00000318522', '1', '929'],
 ['EML4', 'ENST00000318522', '903+188', '903+220'],
 ['ALK', 'ENST00000389048', '4080', '6220'],

 ['FUS', 'ENST00000254108', '1', '(608)'],
 ['FUS', 'ENST00000254108', '(819)', '937'],
 ['DDIT3', 'ENST00000547303', '76', '872'],

 ['TCF3', 'ENST00000262965', '1', '1795'],
 ['ins27'],
 ['PBX1', 'ENST00000420696', '454', '6636'],

 ['EML4', 'ENST00000318522', '?'],
 ['ALK', 'ENST00000389048', '?']
]
首先用“(?=[a-zA-Z]”拆分。然后用“[}{.\u r]”拆分


类似于在
上拆分(\{124;\}:r\.\124; u)
?。我也尝试过,问题是“数字”连接到错误的标识符。例如
1_1364_FLI1
,但是1_1364…`应该与
EWSR1
一起使用。如果一行中有很多标识符,我会丢失重新分配emTry@WiktorStribiżew Nice one,但在
ins…
类型的lines@Chris_Rands可悲的是,这是一个在线数据库。最悲哀的是这是最常用的一款。COSMICI希望它对你有用。如果你需要更多关于这个主题的细节或帮助,请随时在这里发表评论。
import re
regex = r"((?<![^_])ins\d+)_|([a-zA-Z]+[0-9]*)\{([^{}]*)\}:r\.([-()?+\d]+)?(?:_([-()?+\d]+))?"
test = ["EWSR1{ENST00000397938}:r.1_1364_FLI1{ENST00000429175}:r.1046_3051",
    "EML4{ENST00000318522}:r.1_929_EML4{ENST00000318522}:r.903+188_903+220_ALK{ENST00000389048}:r.4080_6220",
    "FUS{ENST00000254108}:r.1_(608)_FUS{ENST00000254108}:r.(819)_937_DDIT3{ENST00000547303}:r.76_872",
    "TCF3{ENST00000262965}:r.1_1795_ins27_PBX1{ENST00000420696}:r.454_6636",
    "EML4{ENST00000318522}:r.?_ALK{ENST00000389048}:r.?"]
res = []
for s in test:
    for match in re.finditer(regex, s):
        tmp = []
        for groupNum in range(0, len(match.groups())):
            if match.group(groupNum+1):
                tmp.append(match.group(groupNum+1))
        res.append(tmp)
print(res)
[
 ['EWSR1', 'ENST00000397938', '1', '1364'],
 ['FLI1', 'ENST00000429175', '1046', '3051'],

 ['EML4', 'ENST00000318522', '1', '929'],
 ['EML4', 'ENST00000318522', '903+188', '903+220'],
 ['ALK', 'ENST00000389048', '4080', '6220'],

 ['FUS', 'ENST00000254108', '1', '(608)'],
 ['FUS', 'ENST00000254108', '(819)', '937'],
 ['DDIT3', 'ENST00000547303', '76', '872'],

 ['TCF3', 'ENST00000262965', '1', '1795'],
 ['ins27'],
 ['PBX1', 'ENST00000420696', '454', '6636'],

 ['EML4', 'ENST00000318522', '?'],
 ['ALK', 'ENST00000389048', '?']
]
import re
with open('file.txt') as f:
    for l in f:
        print "\n".join(map(lambda x:" ".join(re.split(r'[}{:._r]',x)),re.split(r'_(?=[a-zA-Z])',l.strip('\n'))))+'\n'