Sublime Forum

Problem with find (regex)

#1

Hi,

I want to use a negative lookahead regular expression. In this case, find the last occurence of an open round bracket.

regex = "\((?!.*\()" search = "print (\"Current line range: \" + str(current_line(..." result = search.find(regex) print result

The result is always -1, so my regualar expression seems to be wrong. How do I define it correctly?

0 Likes

#2

You want to use the re module, do a search for “python re” on google.

You are using the the string’s find method. The find method takes a sub string, not a regex. Find is looking literally for these characters between the quotes “((?!.*()”, it is not doing regular expression.

0 Likes

#3

Thank you, faceless.

I was able to solve it via the re.search() method.

Regards,
Highend

0 Likes