MAC Address Validation Regex
• CronOS Team
regexmac-addressvalidationtutorialnetworking
Need to generate a regex pattern?
Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!
MAC Address Validation Regex
Validate MAC addresses in standard format with 6 groups of 2 hexadecimal digits separated by colons or hyphens.
Pattern Breakdown
regex
^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$
Components
| Component | Description | Matches |
|---|---|---|
^ | Start anchor | Ensures match from string start |
([0-9A-Fa-f]{2}[:-]){5} | First 5 groups | Exactly 5 groups of 2 hex digits + separator |
([0-9A-Fa-f]{2}) | Final group | Last group of 2 hex digits (no trailing separator) |
$ | End anchor | Ensures match to string end |
Character Classes
[0-9A-Fa-f]- Hexadecimal digits: 0-9, A-F, a-f{2}- Quantifier: exactly 2 occurrences{5}- Quantifier: exactly 5 occurrences[:-]- Separator: colon or hyphen()- Capturing groups for structure
Examples
Valid:
00:1B:44:11:3A:B700-1B-44-11-3A-B700:1b:44:11:3a:b7(lowercase)00:1B:44:11:3A:B7FF:FF:FF:FF:FF:FF(broadcast address)
Invalid:
00:1B:44:11:3A(only 5 groups)00:1B:44:11:3A:B7:C8(7 groups)00:1B:44:11:3A:B(last group incomplete)00:1B:44:11:3A:GB(invalid hex character)00 1B 44 11 3A B7(spaces not supported)001B44113AB7(no separators)
Implementation
JavaScript
javascript
const macRegex = /^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/;
macRegex.test('00:1B:44:11:3A:B7'); // true
macRegex.test('00-1B-44-11-3A-B7'); // true
macRegex.test('00:1B:44:11:3A'); // false (incomplete)
macRegex.test('00:1B:44:11:3A:GB'); // false (invalid hex)
Python
python
import re
mac_regex = r'^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$'
bool(re.match(mac_regex, '00:1B:44:11:3A:B7')) # True
bool(re.match(mac_regex, '00-1B-44-11-3A-B7')) # True
bool(re.match(mac_regex, '00:1B:44:11:3A')) # False (incomplete)
Go
go
macRegex := regexp.MustCompile(`^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$`)
macRegex.MatchString("00:1B:44:11:3A:B7") // true
macRegex.MatchString("00-1B-44-11-3A-B7") // true
macRegex.MatchString("00:1B:44:11:3A") // false (incomplete)
Limitations
- Mixed separators: Doesn't prevent mixing colons and hyphens (e.g.,
00:1B-44:11-3A:B7) - No space support: Doesn't support space-separated format
- No dot notation: Doesn't support Cisco-style dot notation (e.g.,
0000.1b44.113a) - Format only: Validates format, not actual MAC address validity
- No multicast/broadcast validation: Doesn't check for special MAC addresses
When to Use
- MAC address format validation
- Network device configuration
- Hardware address input forms
- Network management systems
- When you need standard MAC format checking
For production, consider:
- Supporting space-separated format if needed
- Supporting dot notation for Cisco devices
- Preventing mixed separators
- Validating multicast/broadcast addresses if required
- Normalizing to a single format for storage
Need to generate a regex pattern?
Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!