python - Additional length check to given RegEx -


i have following regex:

valueregex = re.compile('^(?p<devid>.{2})(?p<cmd>tmpa|batt)(?p<value>-?[\d\.]+)-*$', re.i) 

now, want additionally check, if given string has exact length of 11 chars. normally, wouldn't problem, in case, can't figure out how implement it. several attempts didn't work , i'm out of ideas. :/

perhaps, i'm blind see easy solution :)

here's function need string checked:

def parsemessages(llapmsg):     rawmsgs = llapmsg.split('a')     result = []      data = [valueregex.match(val) val in rawmsgs]      val in data:         if val not none:             result.append(val.groupdict())      return result 

it'll easier test length of match afterwards.

with optional -? part, plus both + , * multipliers in expression, altering match 11 characters going be.. tricky.

you use matchobject .start() , .end() methods access length of match:

for val in data:     if val not none , (val.end() - val.start()) == 11:         result.append(val.groupdict()) 

Comments