Regex Wierdness

Do you have questions about writing plugins or scripts in Python? Meet the coders here.
Post Reply
jprovan
Posts: 5
Joined: Mon Jan 04, 2010 5:02 pm

Regex Wierdness

Post by jprovan » Wed Feb 17, 2010 4:05 pm

I have a bit of code that is failing and I don't know why:

import re

re.sub(r'^[ \t\r\n]+|[ \t\r\n]+$', '', self.LG_Serial_1_result)

m = re.match(r'^a \d{1,2} OK(\d{1,2})x$', self.LG_Serial_1_result)

try:
test = self.LG_Serial_1_result[m.start():m.stop()]

print "Device feedback = " + self.LG_Serial_1_result[m.start():m.stop()]
except:
print "Unkown response - " + self.LG_Serial_1_result



It works in every tester for regex that I have tried, but does not get a match with this code. What am I doing wrong ?

I get Unkown response - a 01 OK01x everytime.

User avatar
jinxdone
Plugin Developer
Posts: 443
Joined: Tue Jan 02, 2007 4:08 pm

Re: Regex Wierdness

Post by jinxdone » Wed Feb 17, 2010 8:41 pm

Try adding print sys.exc_info() in your except clause. When an exception occurs you should see what the error was so you can fix it. Probably the value returned by m.start() or m.stop() is something unexpected.

Code: Select all

import re, sys

re.sub('^[ \t\r\n]+|[ \t\r\n]+$', '', self.LG_Serial_1_result)
m = re.match('^a \d{1,2} OK(\d{1,2})x$', self.LG_Serial_1_result)

try:
    test = self.LG_Serial_1_result[m.start():m.stop()]
    print "Device feedback = " + self.LG_Serial_1_result[m.start():m.stop()]
except:
    print "Unkown response - " + self.LG_Serial_1_result
    print sys.exc_info()
-jinxdone

jprovan
Posts: 5
Joined: Mon Jan 04, 2010 5:02 pm

Re: Regex Wierdness

Post by jprovan » Thu Feb 18, 2010 12:44 am

I added the statement and the output is

(<type 'exceptions.AttributeError'>, AttributeError('stop',), <traceback object at 0x047F30F8>)

jprovan
Posts: 5
Joined: Mon Jan 04, 2010 5:02 pm

Re: Regex Wierdness

Post by jprovan » Thu Feb 18, 2010 12:54 am

I found it. It is m.end()...not m.stop(). Thanks for the help.

Post Reply