I needed an easy way to validate a MAC address in a bash script that generated a unique hostname based on the MAC address of the system.
This gem did the trick: [code lang="bash"] echo ":3e:a" | egrep "^([0-9a-f A-F]:)[0-9a-f A-F]$" [/code] In the event that there was a problem getting the MAC address (e.g., faulty NIC or unstable device driver), I generate a random hostname instead of basing the hostname generation on the MAC.
:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f] | \[\x01-\x09\x0b\x0c\x0e-\x7f]) ) \]) 1 I've used this technique multiple times. Also I suspect that soon all the "valid" emails that don't pass the check above (ie HTML5 validation) will be changed to be compatible with it.
I build web applications using open source software, especially Django.
Started my career doing graphic design for newspapers and magazines in the '90s.
There are three common tasks related to storing mac address in the database as char(12) (it is feasible to use bigint instead but in our case we opted for a text type).
In our case user is allowed to enter mac address the way s/he wants: 000CF15698AD 00:0C: F: AD 00-0C-F1-56-98-AD So the perfect solution would be to apply regular expression like this one: /([a-f A-F0-9][:|\-]?
As an additional example, see the "almost RFC 822 compatible regex" in this answer.
Keep it simple, and don't mark some e-mail addresses that are actually valid as invalid.
I liked petdance's solution ( ), but didn't use it because in my application I only wanted to allow ':' as the separator.
Thanks to the helpful CB people who assisted me with this: blakem, demerphq, davorg & Petruchio.
Try this chunk of code out: ) __END__ My MAC address is 00-00-00-00-00-00 over there, and it's : over here, and the date/time happens to be 2001-05-25-. The thought of that honestly had not crossed my mind. It's not very often that I run across spots to backref in a match or the LHS of a substitution. -marius 1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right.
When he states that something is impossible, he is very probably wrong. Hi marius, The thread "One for the regexp fans" from last fall also deals with parsing MAC addresses, albeit from a little different angle.
Monks, Does anyone have a regex that will successfully match a MAC address that is better than the following?