2

I have strings like this,

Protein XVZ [Human]
Protein ABC [Mouse]
Protein CDY [Chicken [type1]]
Protein BBC [type 2] [Bacteria]

Output should be,

Human
Mouse
Chicken [type1]
Bacteria

Thus, I want everything inside the last pair of braces. Braces that precede that pair must be ignored as in last example. Is there an effective way to do this in Python? Thanks in advance for your help.

4

1 回答 1

1

how about this:

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
于 2014-06-11T03:13:57.977 回答