Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Simple RE answer

by tilly (Archbishop)
on Aug 05, 2000 at 00:18 UTC ( #26262=note: print w/ replies, xml ) Need Help??


in reply to One for the regexp fans

Hmmm..

$mac =~ s/(^|\.)([0-9a-f]?)(?=[0-9a-f])/$2 || 0/eg;
UPDATE

That solves the original problem. ybiC pointed out that another format mac addresses come in has groups of 4 characters, separated by :. The amended question was whether both formats can be handled together. The following does that:

$mac =~ s/[^0-9a-f]*([0-9a-f]?)([0-9a-f])/($1 || 0) . $2/eg;
In both cases the delimiter is gobbled by not being cached in a backref, and if it does not appear in even groups then a 0 will appear.

Danger alert. In the pair "05" the 0 gets replaced, so if you wanted to use this to insert anything other than 0 you would need to not just check the truth of $1, but rather it's length.

Cheers,
Ben


Comment on Simple RE answer
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://26262]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2014-08-21 09:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (129 votes), past polls