Regular Expressions 101

Community Patterns

Community Library Entry

0

Regular Expression
PCRE (PHP <7.3)

/
^(?:(?:(?:(3)\.?([01]2))\.?(\d{1,4})?)|(?:(3)\.?(1)\.?([469]\d?))|([1-2][0-9]|0?[1-9])\.?(?:(1[0-2]|0?[1-9])\.?(\d{1,4})?|(0)?)|(?:(31)\.?(?:(?:(0?[13578]|1[02])\.?(\d{1,4})?)|(0)?))|(?:(30)\.?(?:(?:(0?[13456789]|1[0-2])\.?(\d{1,4})?)|(0)?))|([0-9]))$
/
gm

Description

this regex is able to validate a date (format dd.mm.yyyy) in every state of user typing. i use it to ensure that the user isn't able to type in an invalid date at all (except from a leap year check which is done afterwards)

The regex also makes assumptions of 'missed' separators so 1234 is captured in groups which can then be transformed to \1.\2.\3 -> 12.3.4

It was intensively but not exhaustivly tested

Submitted by anonymous - 9 years ago