Regular Expressions 101

Community Library Entry

1

Regular Expression
PCRE2 (PHP >=7.3)

/
^(([D]{1,4}|[M]{1,4}|[Y]{2}|[Y]{4})(?<SEPARATOR>[-.\/_] )([D]{1,4}|[M]{1,4}|[Y]{2}|[Y]{4})(\k<SEPARATOR>)([D]{1,4}|[M]{1,4}|[Y]{2}|[Y]{4}))$
/
gm

Description

For the day: d Displays the day as a number without a leading zero (1-31). dd Displays the day as a number with a leading zero (01-31). ddd Displays the day as an abbreviation (Sun-Sat) dddd Displays the day as a full name (Sunday-Saturday)

For the month: m Displays the month as a number without a leading zero (1-12). mm Displays the month as a number with a leading zero (01-12). mmm Displays the month as an abbreviation (Jan-Dec). mmmm Displays the month as a full name (January-December).

For the year: yy Displays the year as a two-digit number (00-99). yyyy Displays the year as a four-digit number (0000-9999).

Additionally, the supported separators are:

  1. Hyphen (-)
  2. Underscore (_)
  3. Period (.)
  4. /
Submitted by EYA GHACHEM - a year ago