Home Download Buy Blog Forum Support

Problem with find (regex)

Problem with find (regex)

Postby highend on Fri Dec 07, 2012 12:22 am

Hi,

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

Code: Select all
         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?
highend
 
Posts: 93
Joined: Fri Jan 20, 2012 2:47 pm

Re: Problem with find (regex)

Postby facelessuser on Fri Dec 07, 2012 12:50 am

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.
facelessuser
 
Posts: 1448
Joined: Tue Apr 05, 2011 7:38 pm

Re: Problem with find (regex)

Postby highend on Fri Dec 07, 2012 4:26 pm

Thank you, faceless.

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

Regards,
Highend
highend
 
Posts: 93
Joined: Fri Jan 20, 2012 2:47 pm


Return to Technical Support

Who is online

Users browsing this forum: Yahoo [Bot] and 15 guests