我对文本条目应用了许多regex模式,我希望它们能自动输入到一个列表中。你知道吗
例如
import re
raw='NIH05484225_1_binders_list ID_054345 Proteincomplex /D1/ERBH1_ERV_ACWX'
regex= '.*Proteincomplex /[\S]+\/([\S]+)_([\S]+)_([\S]+)'
regex=re.compile(regex)
result = re.match(regex, raw)
answer=[]
answer.append (result.group(1))
answer.append (result.group(2))
answer.append (result.group(3))
print (answer)
有没有办法让answer
收集所有捕获的结果?
如在
answer.extend (result.allgroups()) # does not work, of course
您可以使用^{} :
因此,您可以这样修改代码:
注意:您不需要用字符类包装每个
\S
,使用\s+
而不是空格可以匹配任何一个或多个空格(它被认为更可读且故障安全,特别是如果您计划稍后使用re.VERBOSE
/re.X
在模式中插入注释或格式化空格)。你知道吗相关问题 更多 >
编程相关推荐