Step: Named capturing groups

Named capturing groups

(?<name> ... ) gives a capturing group a name, so you refer to results by meaning instead of by position.

Why it matters: named groups make patterns self-documenting and stop you from miscounting group numbers as a pattern grows.

Example: (?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2}) splits a date into named parts. This exercise returns an object of the named groups.

Your turn: capture the date parts with the named-group pattern above.

Pitfall: every group name must be unique within the pattern, and names follow identifier rules (letters, digits, underscore; not starting with a digit).

Input:
2024-05-01
Expected:
[
  {
    "year": "2024",
    "month": "05",
    "day": "01"
  }
]
Step 11 of 11