Regular expression for dd / mm

Please help me with regex to confirm the following format

dd/mm

This is for checking the "Birthday" field, and a year is not required.

thank

+3
source share
3 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 correspond to valid days / months. He will always correspond on February 29, since he does not know the year.

By the way, I made this regex using RegexMagic (it is too tedious to do this manually - another sign that this is more likely to work for a date / time analyzer).

+2
source
new Regex(@"^\d{2}/\d{2}$")

or

new Regex(@"^\d\d/\d\d$")

\drepresents a number, but {2}indicates that it should be repeated twice.

, , DateTime.TryParseExact:

DateTime date;
bool valid;
valid = DateTime.TryParseExact("00/00", "dd/MM", null, DateTimeStyles.None, out date); // false
valid = DateTime.TryParseExact("30/02", "dd/MM", null, DateTimeStyles.None, out date); // false
valid = DateTime.TryParseExact("27/02", "dd/MM", null, DateTimeStyles.None, out date); // true

. , .

0
^(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])$

but this will allow, for example, 30/02

0
source

Source: https://habr.com/ru/post/1747148/


All Articles