change mac address formatting in a regex

E

edward.nigma

Good evening... I'm trying to construct a way to transform a mac
address written in this format...

0:3:e2:12:4e:54

To this:

0003E2124E54

....in a single regular expression. Actually the change in
capitalization isn't very important, because I'm not sure if that can
be done in the same regular expression that would also be adding
additional zeros to the segments.

I have different methods of doing this using loops that delimit the
data already in place, but I now need to be able to do this through a
single regular expression and it seems there should be a way!
Thank you, I appreciate it!
 
A

A. Sinan Unur

(e-mail address removed) wrote in @c13g2000cwb.googlegroups.com:
Good evening... I'm trying to construct a way to transform a mac
address written in this format...

0:3:e2:12:4e:54

To this:

0003E2124E54

...in a single regular expression.

Is there a good reason why it needs to be done using a regular
expression?

print join '',
map { sprintf '%2.2X', hex $_ } split ':', '0:3:e2:12:4e:54';

Sinan
 
U

Uri Guttman

en> Good evening... I'm trying to construct a way to transform a mac
en> address written in this format...

en> 0:3:e2:12:4e:54

en> To this:

en> 0003E2124E54

en> I have different methods of doing this using loops that delimit the
en> data already in place, but I now need to be able to do this through a
en> single regular expression and it seems there should be a way!

why? and if i show it to you will you learn from it or just cut and
paste it and not understand the code?

what if no regex was used?

what if cool perl was used but you couldn't understand it?

what if it is untested (as these are)?

let's see how you handle these:

$mac =~ s/(\w+):?/sprintf '%02x', hex $1/ge ;

$mac = join '', map { sprintf '%02x', hex $_ } split /:/, $mac ;

now for the gang: let's see how many fun ways there are to do this.

uri
 
A

Arndt Jonasson

Uri Guttman said:
en> Good evening... I'm trying to construct a way to transform a mac
en> address written in this format...

en> 0:3:e2:12:4e:54

en> To this:

en> 0003E2124E54

en> I have different methods of doing this using loops that delimit the
en> data already in place, but I now need to be able to do this through a
en> single regular expression and it seems there should be a way!

[...]
$mac =~ s/(\w+):?/sprintf '%02x', hex $1/ge ;

$mac = join '', map { sprintf '%02x', hex $_ } split /:/, $mac ;

now for the gang: let's see how many fun ways there are to do this.

I put in the uppercasing too.

$mac =~ s/(\w+):?/substr "\U0$1", -2/eg;

$mac =~ s/(\w+):?/"0" x (2-length $1) . "\U$1"/ge;

The second one is not so robust - it assumes there are no redundant
leading zeroes.
 
J

John W. Krahn

Good evening... I'm trying to construct a way to transform a mac
address written in this format...

0:3:e2:12:4e:54

To this:

0003E2124E54

...in a single regular expression. Actually the change in
capitalization isn't very important, because I'm not sure if that can
be done in the same regular expression that would also be adding
additional zeros to the segments.

I have different methods of doing this using loops that delimit the
data already in place, but I now need to be able to do this through a
single regular expression and it seems there should be a way!

$ perl -le'

$_ = q/0:3:e2:12:4e:54/;

print;
s/([[:xdigit:]]+|:)/ $1 eq ":" ? "" : sprintf "%02X", hex $1 /eg;
print;
'
0:3:e2:12:4e:54
0003E2124E54



John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top