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.
Notice: This forum has been recovered from an old backup, so some content, links, and dates may be outdated. The forum is currently read-only while we restore sign-in and registration functionality. Details
If you find this forum valuable and would like to help keep it online, donations to help cover hosting and domain costs are greatly appreciated, but never expected. You can support the forum through Buy Me a Coffee or Ko-fi. Thank you for helping preserve the EventGhost community.
If you find this forum valuable and would like to help keep it online, donations to help cover hosting and domain costs are greatly appreciated, but never expected. You can support the forum through Buy Me a Coffee or Ko-fi. Thank you for helping preserve the EventGhost community.
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.