正则表达式 分组命名 日期 提取

root
abc abc
  • 25 Oct

import re

pattern = r'(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})'
date_str = "2023-04-01"
match = re.match(pattern, date_str)

if match:
year = match.group('year')
month = match.group('month')
day = match.group('day')
print(f"Year: {year}, Month: {month}, Day: {day}")