in reply to Re: Re: Converting MAC Address
in thread Converting MAC Address
Try
$foo = "0:0A:0C:B:B8:F"; $foo =~ s/([0-9A-F]+)(?::|$)/length($1) < 2?"0$1":$1/ge; print $foo 000A0C0BB80F
With your version [0-9A-F]+ can just as easily match one hex char as two as the :? part is optional (so that you will match at the end of line. Therefore, it matches the ...:B8:... in two chunks instead of one.
The version above makes the : or end-of-string mandatory (?::|$) and forces the B8 part to be matched as a single piece rather than two.
Not necessarially the best way to do it, but it corrects the problem you've identified.
You can also simplify the right-hand side of the s/// somewhat like this:
$foo =~ s/([0-9A-F]+)(?::|$)/substr "0$1", -2/ge;
Examine what is said, not who speaks.
The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Re: Converting MAC Address
by Enlil (Parson) on Jan 11, 2003 at 01:59 UTC |