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.

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 »

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 »

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 »

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 »

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