The message may contain a date in the format dd.mm.yyyy. In the specified format, dd is an integer from 1 to 31, mm is an integer from 1 to 12, and yyyy is an integer from 1900 to 2012 (if some part of the format is broken, then this substring is dates are not considered). Find all dates that relate to the current year.

I got the following regular expression:

[1-31]\.[1-12]\.[1900-2012] 

I can not understand why the dates corresponding to this format are not recognized.

What should I fix? What is wrong here?

  • one
    Permissible characters (numbers) are indicated to square brackets, not numbers - Kirill Malyshev
  • one
    [1-31] means 1 to 3, i.e. 1, 2, 3 last 1 is ignored as it is already in the set. in any case, all that in square brackets means only one character. for example, from 1 to 31, the expression will be: (0?[1-9]|[12][0-9]|3[01])\. Note the `\` before the stop will mean any character - Mike
  • thanks, but what does the entry "0?" mean ? - Gadget

3 answers 3

Valid characters are listed in square brackets, including numbers, but not numbers. In your case, the expression will be:

(0?[1-9]|[12]\d|3[01])\.(0?[1-9]|1[0-2])\.(19\d\d|20(0\d|1[0-2]))

  • thanks a lot) can you say what does the entry "0?" mean? - Gadget
  • one
    ? means that 0 or 1 character is allowed, indicated on the left to the left. For example, + means that we allow 1 or more characters indicated on the left, in a row. ru.wikibooks.org/wiki/… - Kirill Malyshev

Taking into account the comments of Cyril Malyshev and Mike, the expression was corrected:

 ([1-9]|([1-2][0-9])|([3][0-1]))\.([1-9]|10|11|12)\.((19[0-9][0-9])|(20((0[0-9])|10|11|12))) 

Of course, it turned out not as beautiful as Cyril Malyshev and Mike, it came out very long, but I am glad that this option also worked :)

Thank you for helping to fix the error :)

It remains to understand the record, which means the record "0?" :)

  • one
    "0?" - zero may or may not be) - Garrus_En

Shorter recording with \d :

 (\d|([1-2]\d)|([3][0-1]))\.([1-9]|10|11|12)\.((19\d\d)|(20((0\d)|10|11|12)))