简体   繁体   中英

last regex match in Python

I would like to capture NP_054520.1 (last brackets). Unfortunately, the below code catch the first one.

>>> import re
>>> a="cytochrome b6/f complex subunit V %28plastid%29  %28NP_054520.1%29"
>>> re.search(r'%28(.*?)%29', a).group(1)
'plastid'

What did I miss?

Thank you in advance,

>>> import re
>>> a="cytochrome b6/f complex subunit V %28plastid%29  %28NP_054520.1%29"
>>> re.search(r'.*%28(.*)%29', a).group(1)

'NP_054520.1'

Would you please try the following:

import re
a = "cytochrome b6/f complex subunit V %28plastid%29  %28NP_054520.1%29"
m = re.findall(r'%28(.*?)%29', a)
print(m[-1])     # last element of the list "m"

which outputs:

NP_054520.1

As there are no spaces between %28 and %29 , you could also capture one or more non whitespace characters using \\S+ and assert the end of the string \\Z

%28(\S+)%29\Z

See a regex demo and a Python demo

import re
a="cytochrome b6/f complex subunit V %28plastid%29  %28NP_054520.1%29"
print(re.search(r'%28(\S+)%29\Z', a).group(1))

Output

NP_054520.1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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