[Israel.pm] A regexp question

Ishay Inbar ishay at checkpoint.com
Sun May 16 08:15:29 PDT 2004


This is a simple regexp I thought should work, and for some reason it doesn't.

I have a MAC address from a machine, which is mostly 8 couples of [a-f][A-F][0-9] separated with "-" or ":" sign. For example:

In some platforms, though, in case the left char in a couple is zero, you just get the left value, for example from Solaris:
8:0:20:9f:22:78, where the first and the second couples start with 0.

So I need to get 16 chars string, but in order not to loose data I need to first fill the couples, and then remove the : or – sign.
What I did was:

my $zero = '0';

$mac =~ s/(^|[^0-9A-Fa-f])([0-9A-Fa-f])([^0-9A-Fa-f])/$1$zero$2$3/g;

For some reason this didn't work, and only replaced the first couple with the missed value. A simple solution was:

while ($mac =~ s/(^|[^0-9A-Fa-f])([0-9A-Fa-f])([^0-9A-Fa-f])/$1$zero$2$3/g) {}

Can someone tell me why the global flag didn't work in this situation ?


More information about the Perl mailing list