Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression for dd/mm

Tags:

c#

.net

regex

Please help me with a regular expression to validate the following format

dd/mm

This is for validating a Birthday field and the year is not required.

Thanks

like image 993
swapna Avatar asked Feb 01 '26 12:02

swapna


1 Answers

bool foundMatch = false;
foundMatch = Regex.IsMatch(SubjectString, 
    @"^(?:
     (?:[12][0-9]|0?[1-9])/(?:0?2|Feb(?:ruary)?)
     |
     (?:30|[12][0-9]|0?[1-9])/
      (?:
       (?:0?[469]|11)
       |
       Apr(?:il)?|June?|Sep(?:tember)?|Nov(?:ember)?
      )
     |
     (?:3[01]|[12][0-9]|0?[1-9])/
      (?:
       (?:0?[13578]|1[02])
       |
       Jan(?:uary)?|Mar(?:ch)?|May|July?|Aug(?:ust)?|Oct(?:ober)?|Dec(?:ember)?
      )
     )$",  
    RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);

will match valid days/months. It will always match Feb 29 since it doesn't know the year.

By the way, I made this regex using RegexMagic (much too tedious to do this manually - another sign that this is rather a job for a date/time parser).

like image 64
Tim Pietzcker Avatar answered Feb 03 '26 00:02

Tim Pietzcker