Mac Address insert colon

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
mgichoga
Forum Newbie
Posts: 3
Joined: Wed Oct 03, 2007 9:10 pm

Mac Address insert colon

Post by mgichoga »

Hi,

I'm seeking help with a peculiar problem. i assigned some clerical staff to enter some computer inventoy and in their wisdom they ommitted the colon in the mac addresses. I have over 2000 records and I'm fairly a novice in regex and was wondering if there is a quick way to insert a colon so it could match a normal mac address e.g

0030488400C8 to 00:30:48:84:00:C8

Any help will be greatly appreciated
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'd probably use str_split() instead.

What have you tried as far as regular expressions... (if you wish to continue this route) ?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

I would follow feyd's suggestion by using str_split() as an argument for implode()
There are 10 types of people in this world, those who understand binary and those who don't
mgichoga
Forum Newbie
Posts: 3
Joined: Wed Oct 03, 2007 9:10 pm

Post by mgichoga »

now that you mentioned I remembered there is a substr function in perl. This is what I ended up doing in perl. Obviously I know there is a better way of doing this than this method so any suggestions in regex/php/perl are welcome

$mac="002145783456";
$mac1=substr($mac,0,2);
$mac2=substr($mac,4,2);
$mac3=substr($mac,6,2);
$mac4=substr($mac,8,2);
$mac5=substr($mac,10,2);

$pad="$mac1:"."$mac2:"."$mac3:"."$mac4:"."$mac5";
print "$mac\n";
print "$pad\n";
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

substr() is available, but the function I already linked to is more useful.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

Did you look at the functions feyd and me pointed to?
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post by GeertDD »

And if you really wanted to use a regex, try one like this:

Code: Select all

preg_replace('/..(?!$)/', '$0:', '0030488400C8')
mrkite
Forum Contributor
Posts: 104
Joined: Tue Sep 11, 2007 4:19 am

Post by mrkite »

edit: never mind, GeertDD beat me to it.
mgichoga
Forum Newbie
Posts: 3
Joined: Wed Oct 03, 2007 9:10 pm

Post by mgichoga »

Thanks to all of ya.

Appreciate it.
Post Reply