Pattern Breakdown
Understanding the regex pattern ^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$:
- ^ - Start of string
- $ - End of string
- [...] - Character class
- (...) - Capturing group
- {n,m} - Quantifier (n to m times)
Examples
| Input | Result | Explanation |
|---|---|---|
00:1A:2B:3C:4D:5E |
✓ Match | Colon separated |
00-1A-2B-3C-4D-5E |
✓ Match | Hyphen separated |
Code Examples
JavaScript
JavaScript
const pattern = /^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/;
const text = "00:1A:2B:3C:4D:5E";
// Test if string matches
if (pattern.test(text)) {
console.log("Valid!");
}
// Extract matches
const matches = text.match(pattern);
console.log(matches);
Python
Python
import re
pattern = r'^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$'
text = "00:1A:2B:3C:4D:5E"
# Test if string matches
if re.match(pattern, text):
print("Valid!")
# Find all matches
matches = re.findall(pattern, text)
print(matches)
PHP
PHP
<?php
$pattern = '/^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/';
$text = "00:1A:2B:3C:4D:5E";
// Test if string matches
if (preg_match($pattern, $text)) {
echo "Valid!";
}
// Find all matches
preg_match_all($pattern, $text, $matches);
print_r($matches);
?>
Java
Java
import java.util.regex.*;
String pattern = "^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$";
String text = "00:1A:2B:3C:4D:5E";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(text);
if (m.matches()) {
System.out.println("Valid!");
}
Use Cases
Common applications: Network configuration, device identification
- Form validation and input sanitization
- Data extraction and parsing
- Search and replace operations
- API request/response validation
- Log file analysis
Frequently Asked Questions
What is the regex pattern for mac address regex?
The regex pattern is: ^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$. Validate MAC addresses with colon or hyphen separators.
How do I use the mac address regex pattern in JavaScript?
In JavaScript: const regex = new RegExp('^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$'); or const regex = /^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/;
Does this pattern work in Python?
Yes, use: import re; pattern = re.compile(r'^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$') in Python. Most regex patterns are cross-language compatible.
What are common use cases for mac address regex?
Network configuration, device identification
Related Regex Patterns
More Regex Tools
GetDevUtils offers a suite of developer tools including:
- Regex Tester - Test and debug regular expressions
- JSON Formatter - Format and validate JSON
- Base64 Encoder/Decoder - Encode and decode Base64
- URL Encoder/Decoder - Encode and decode URLs