0

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)']

1
  • If you expect 1 single match, why use re.findall? Use re.search. Are those parentheses always at the end of the string? Commented Nov 24, 2016 at 13:25

1 Answer 1

1

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

'^.*?(\([A-Z0-9]+\))$'
Sign up to request clarification or add additional context in comments.

1 Comment

The ^.*? is redundant, r'\([^()]+\)$' should be enough.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.