python正则表达式子菜单

2024-06-16 13:29:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要在大量字符串中找到“taxid”的值,类似于下面给出的一个。对于此特定字符串,“taxid”值为“9606”。我需要抛弃所有其他的东西。“taxid”可能出现在文本中的任何位置,但后面总是跟着“:”然后是数字。在

score:0.86|taxid:9606(Human)|intact:EBI-999900

如何在python中为此编写正则表达式。在


Tags: 字符串文本数字scorehumanebitaxidintact
2条回答
for line in lines:
    match = re.match(".*\|taxid:([^|]+)\|.*",line)
    print match.groups()
>>> import re
>>> s = 'score:0.86|taxid:9606(Human)|intact:EBI-999900'
>>> re.search(r'taxid:(\d+)', s).group(1)
'9606'

如果有多个taxid,请使用re.findall,这将返回所有匹配项的列表:

^{pr2}$

相关问题 更多 >