Pattern Breakdown
Understanding the regex pattern ^\d{4}-\d{2}-\d{2}$:
- ^ - Start of string
- $ - End of string
- \d - Any digit (0-9)
- {n,m} - Quantifier (n to m times)
Examples
| Input | Result | Explanation |
|---|---|---|
2024-01-15 |
✓ Match | Valid ISO date |
2024-12-31 |
✓ Match | End of year |
01-15-2024 |
✗ No Match | US format - wrong order |
2024/01/15 |
✗ No Match | Wrong separator |
Code Examples
JavaScript
JavaScript
const pattern = /^\d{4}-\d{2}-\d{2}$/;
const text = "2024-01-15";
// 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'^\d{4}-\d{2}-\d{2}$'
text = "2024-01-15"
# 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 = '/^\d{4}-\d{2}-\d{2}$/';
$text = "2024-01-15";
// 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 = "^\\d{4}-\\d{2}-\\d{2}$";
String text = "2024-01-15";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(text);
if (m.matches()) {
System.out.println("Valid!");
}
Use Cases
Common applications: APIs, databases, international applications
- 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 iso 8601 date regex?
The regex pattern is: ^\d{4}-\d{2}-\d{2}$. Match ISO 8601 date format YYYY-MM-DD. The international standard for date representation.
How do I use the iso 8601 date regex pattern in JavaScript?
In JavaScript: const regex = new RegExp('^\\d{4}-\\d{2}-\\d{2}$'); or const regex = /^\d{4}-\d{2}-\d{2}$/;
Does this pattern work in Python?
Yes, use: import re; pattern = re.compile(r'^\d{4}-\d{2}-\d{2}$') in Python. Most regex patterns are cross-language compatible.
What are common use cases for iso 8601 date regex?
APIs, databases, international applications
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