From: Mark Hubbart Date: 2004-02-04T10:48:37+09:00 Subject: Re: String.hex and valid mac addresses : a cleaner way ? Hmmm... It didn't seem to make it through the first time... I'll try again On Feb 3, 2004, at 12:58 PM, Theodore Knab wrote: > Is there a cleaner way to doing this ? If this code is doing what I think it is, it's checking to make sure that the string is composed of a series of two hex digits separated by colons. If so, you could do this: def valid_mac?(addy) addy =~ /^[a-fA-F0-9]{2}(\:[a-fA-F0-9]{2})*$/ ? true : false end If you need a certain number of sections (ie: 4 "00:00:00:00" or 6 "00:00:00:00:00:00"): def valid_mac?(addy,sections) addy =~ /^[a-fA-F0-9]{2}(\:[a-fA-F0-9]{2}){#{sections-1}}$/ ? true : false end Note that you need the start and end anchors (/^$/), or it matches good mac addresses that are tucked inside bad ones: "01:23:45:FOO!!:67:89" would match the first part and the last part.