簡體   English   中英

提取最后一次出現的括號之間的文本

[英]Extract text between last occurrence of braces

我有這樣的琴弦

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

輸出應該是

Human
Mouse
Chicken [type1]
Bacteria

因此,我希望所有內容都在最后一副括號內。 與上一個示例一樣,必須忽略該對之前的大括號。 有沒有有效的方法可以在Python中做到這一點? 在此先感謝您的幫助。

這個怎么樣:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM