简体   繁体   English

如何使用正则表达式从带有多个括号的字符串中查找字符串?

[英]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)'] 结果:['((18xp)']

You have to use a regexp signs indicating the start and the end of the line. 您必须使用正则表达式符号来指示行的开始和结束。
Try: 尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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