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.
Regex Wierdness
Re: Regex Wierdness
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.
-jinxdone
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()
Re: Regex Wierdness
I added the statement and the output is
(<type 'exceptions.AttributeError'>, AttributeError('stop',), <traceback object at 0x047F30F8>)
(<type 'exceptions.AttributeError'>, AttributeError('stop',), <traceback object at 0x047F30F8>)
Re: Regex Wierdness
I found it. It is m.end()...not m.stop(). Thanks for the help.