Named Capture Group (Date)
Demonstrate named capture groups by extracting year/month/day from ISO-style dates.
Try it in RegexPro →Available in
JavaScript / ECMAScript code
const re = new RegExp("(?<year>\\d{4})-(?<month>\\d{2})-(?<day>\\d{2})", "g");
const input = "Born 1990-05-21, hired 2018-09-10";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
Python (re) code
import re
pattern = re.compile(r"(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})")
input_text = "Born 1990-05-21, hired 2018-09-10"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
Go (RE2) code
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})`)
input := `Born 1990-05-21, hired 2018-09-10`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
Pattern
(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2}) (flags: g)Raw source: (?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})
How it works
Examples
Input
Born 1990-05-21, hired 2018-09-10Matches
1990-05-212018-09-10
Input
Today is 2026-04-25Matches
2026-04-25
Input
no dates hereNo match
—Common use cases
- •Self-documenting regex in production code
- •Extracting structured fields from log lines
- •Form field validation with named outputs
- •Building parsers without positional bookkeeping
Related patterns
Non-Capturing Group (Image URL)
Text ProcessingUse non-capturing groups `(?:...)` to alternate without polluting the captured-groups list.
C-Style Block Comment
Text ProcessingMatch C-style /* ... */ block comments across multiple lines.
Java Package Declaration
Text ProcessingMatch Java `package com.example.app;` declarations and capture the dotted package path.
Keep-a-Changelog Entry Header
Text ProcessingMatch Keep-a-Changelog style version headers like `## [1.2.3] - 2024-01-15` or `## 2.0.0`.