我有这样的字符串,
Protein XVZ [Human]
Protein ABC [Mouse]
Protein CDY [Chicken [type1]]
Protein BBC [type 2] [Bacteria]
输出应该是,
Human
Mouse
Chicken [type1]
Bacteria
因此,我想要最后一对括号内的所有东西。必须忽略该对之前的大括号,如上例所示。有没有一种有效的方法在Python中执行此操作?在此先感谢您的帮助。
答案 0 :(得分:1)
怎么样:
import re
list = ["Protein XVZ [Human]","Protein ABC [Mouse]","go UDP[3] glucosamine N-acyltransferase [virus1]","Protein CDY [Chicken [type1]]","Protein BBC [type 2] [Bacteria] [cat] [mat]","gi p19-gag protein [2] [Human T-lymphotropic virus 2]"]
pattern = re.compile("\[(.*?)\]$")
for string in list:
match = re.search(pattern,string)
lastBracket = re.split("\].*\[",match.group(1))[-1]
print lastBracket