简体   繁体   中英

How to use regex to find strings from string with multiple brackets?

I am fetching and looping on strings which can have one brackets or multiple as shown below. I want the strings inside the last bracket.

strOne = "This contains (18xp) (23lo) (SerialA)"
strTwo = "This contains (jxp) (SerialB)"
strThree = "Some strings (randomA9)"

I tried to use below code but it only capture first:

regFormat = '(\([A-Z0-9]+\))'
pathReg = re.compile(regFormat)
findr = re.findall(pathReg , strOne)
print(findr)

RESULT : ['(18xp)']

You have to use a regexp signs indicating the start and the end of the line.
Try:

'^.*?(\([A-Z0-9]+\))$'

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