in reply to Regex overlap in MAC address
0:13:46:b:4:31 ^^ First pass ^^^^ Second pass ^^^ Third pass
You're expecting the regexp to match the same : twice. Use a zero-width lookahead to check for the trailing : or end of string.
$mac =~ s/(^|:)([0-9a-fA-F])(?=:|$)/${1}0$2/g;
Better yet, eliminate needless copying.
$mac =~ s/(^|:)(?=[0-9a-fA-F](?::|$))/${1}0/g;
Update: Tested. Fixed bug where : would get removed.
Update: If you really need speed, the following is much faster, but it sacrifices readability:
$mac =~ s/(?<=:)(?=[0-9a-fA-F](?::|$))/0/g; $mac =~ s/^(?=[0-9a-fA-F](?::|$))/0/;
Rate orig fast orig 36251/s -- -42% fast 62533/s 72% --
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Regex overlap in MAC address
by Skeeve (Parson) on Sep 11, 2006 at 20:06 UTC | |
by ikegami (Patriarch) on Sep 11, 2006 at 20:48 UTC | |
by Skeeve (Parson) on Sep 12, 2006 at 07:11 UTC | |
by Anonymous Monk on Sep 12, 2006 at 08:53 UTC | |
by ikegami (Patriarch) on Sep 12, 2006 at 19:11 UTC |