regular expression back references

Peter Abel p-abel at t-online.de
Sat Aug 9 16:17:31 EDT 2003


ruach at chpc.utah.edu (Matthew) wrote in message news:<ec1162c7.0308081412.4c0d2fca at posting.google.com>...
> Greetings, I am having a problem using back references in my regex and
> I am having a difficult time figuring out what I am doing wrong. My
> regex works fine with out the back refs but when I try to use them it
> won't match my sample. It looks to me that I am using them no
> differently then my examples and documentation but to no avail.
> 
> Here is my patteren:
> 
> macExpression = "^[0-9A-F]{1,2}(\:|\.|\-)[0-9A-F]{1,2}\1[0-9A-F]{1,2}\1[0-9A-F]{1,2}\1[0-9A-F]{1,2}\1[0-9A-F]{1,2}$:
> 
> And this is how I am using it:
> 
> matched = re.match(macExpression, macAddress)
> 
> I am trying to match mac addresses in the following formats
> 0:a0:c9:ee:b2:c0, 0-a0-c9-ee-b2-c0 & 0.a0.c9.ee.b2.c0 etc.
> 
> I wasn't sure how to do it but then I read about back references and I
> thought that all was well... Alas If any one could lend a hand I would
> appreciate it very much.
> 
> -matthew

Matching even the silliest format of macAdress, the
following works for me:
>>> silly_adrs=['00-30:65:01.dC:9f', '00:03-93.52.0C-c6', '00-A0:C9.eE:b2.C0']
>>> def mac_adr(any_adr):
... 	return '.'.join(map(lambda x:str(int(x,16)),
                        re.split('[\:\-\.]',any_adr)))
... 
>>> for adress in silly_adrs:
... 	print mac_adr(adress)
... 
0.48.101.1.220.159
0.3.147.82.12.198
0.160.201.238.178.192
>>>

Regards
Peter




More information about the Python-list mailing list