Regular Expressions 101

Community Patterns

Your search did not match anything

Community Library Entry

1

Regular Expression
PCRE2 (PHP >=7.3)

/
^(?:(19[0-9]{2}|[2-9][0-9]{3})-(0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01])|(19[0-9]{2}|[2-9][0-9]{3})-(0[469]|11)-(0[1-9]|[12][0-9]|30)|(19[0-9]{2}|[2-9][0-9]{3})-(02)-(0[1-9]|1[0-9]|2[0-8])|((?:19|[2-9][0-9])(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))-(02)-(29))$
/
gm

Description

This regex validates dates in YYYY-MM-DD format with comprehensive rules:

  • Year: Must be greater than 1900 (1901-9999)
  • Month: Valid range 01-12
  • Day: Validates correct number of days per month:
    • 31 days: January, March, May, July, August, October, December
    • 30 days: April, June, September, November
    • 28 days: February (non-leap years)
    • 29 days: February 29th only in leap years

The regex captures year, month, and day in separate groups (positions vary based on which alternative matches).

Submitted by gh/barabasz - 8 days ago (Last modified 8 days ago)