0

I want to use regular expressions to get the text inside parenthesis in a sentence. But if the string has two or more occurrence, the pattern I am using gets everything in between. I google it and some sources tells me to use negative lookahead and backreference, but it is not working as expected. The examples I found are: Here, here

An example of a string is:

s = "Para atuar no (GCA) do (CNPEM)"

What I want is to get just the last occurrence: "(CNPEM)"

The pattern I am using is:

pattern = "(\(.*\))(?!.*\1)"

But when I run (using python's re module) I get this:

output = (GCA) do (CNPEM)

How can I get just the last occurrence in this case?

1 Answers1

1

You could use re.findall here, and then access the last match:

s = "Para atuar no (GCA) do (CNPEM)"
last = re.findall(r'\(.*?\)', s)[-1]
print(last)  # (CNPEM)
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360