[Israel.pm] A regexp question

Mikhael Goikhman migo at homemail.com
Mon May 17 06:04:59 PDT 2004


On 17 May 2004 15:33:09 +0200, Itzik Lerner wrote:
> 
> try
> 
>   $mac=~s/([\dA-Fa-f]{1,2})[:-]?/sprintf "%02X",$1/eg;

Two technical notes for those who want to learn.  The "[:-]?" substring
does not contribute anything useful in this example, and there is a bug,
a missing hex() function. The correct statement is:

    $mac=~s/([\dA-Fa-f]{1,2})/sprintf "%02X",hex($1)/eg;

And to solve your original task (you need 16 chars string, not 25 chars),
the following statement does this without any compex regexps:

    $m16 = join('', map { sprintf "%02X", hex $_ } split /[:-]/, $mac);

Regards,
Mikhael.

-- 
perl -e 'print+chr(64+hex)for+split//,d9b815c07f9b8d1e'



More information about the Perl mailing list