繁体   English   中英

序列搜索期间的正则表达式错误

[英]Regular Expression Error During Sequence Search

在此先感谢,我正在尝试从 NCBI 中提取作为分类群 ID 的代码的最后几位数字。 我想要这个标签中的粗体数字,但是这些数字的长度和值是可变的:

标签: URS0000D94775_60169

代码:

import re
taxID = ()
#strip accession numbers into string
mount = open ('mount.txrt', 'r')
accessions = (re.findall ("URS\S{6}", mount))
     for i in accessions:
     taxID.append (i)
 #parse taxa id's from string        
 taxas = ()
 taxas.append (re.findall ('\_?\d+', taxID)) 
 print ( mount)

re.findall与下面的正则表达式一起使用:

import re
tag = 'URS0000D94775_60169'
tax_id = re.findall(r'\d+$', tag)[0]
print(tax_id)
# 60169

由于您的模式中有一个可选的_并且您想要匹配 URS 之后的数字,您可以使用

URS(?:.*?\D)?(\d+)$

正则表达式演示

import re
s = "URS0000D94775_60169"
print(re.findall("URS(?:.*?\D)?(\d+)$", s))

Output

60169

如果必须存在下划线:

URS[^_]*_(\d+)$

正则表达式演示

import re
s = "URS0000D94775_60169"
print(re.findall("URS[^_]*_(\d+)$", s))

Output

60169

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM