Skip to Main Content
Wyra?enia regularne. Receptury
book

Wyra?enia regularne. Receptury

by Jan Goyvaerts, Steven Levithan
May 2010
Intermediate to advanced content levelIntermediate to advanced
520 pages
17h 44m
Polish
Helion
Content preview from Wyra?enia regularne. Receptury

4.5. Bardziej restrykcyjna weryfikacja tradycyjnych formatów zapisu daty

Problem

Chcemy weryfikować daty w tradycyjnych formatach mm/dd/yy, mm/dd/yyyy, dd/mm/yy oraz dd/mm/yyyy. Tym razem jednak chcemy odrzucać nieprawidłowe daty, jak 31 lutego.

Rozwiązanie

C#

Numer miesiąca przed numerem dnia:

DateTime foundDate;
Match matchResult = Regex.Match(SubjectString,
    "^(?<month>[0-3]?[0-9])/(?<day>[0-3]?[0-9])/" +
    "(?<year>(?:[0-9]{2})?[0-9]{2})$");
if (matchResult.Success) {
    int year = int.Parse(matchResult.Groups["year"].Value);
    if (year < 50) year += 2000;
    else if (year < 100) year += 1900;
    try {
        foundDate = new DateTime(year,
            int.Parse(matchResult.Groups["month"].Value),
            int.Parse(matchResult.Groups["day"].Value));
    } catch {
        // Nieprawidłowa data. } } ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mikrousługi oparte na zdarzeniach

Mikrousługi oparte na zdarzeniach

Adam Bellemare

Publisher Resources

ISBN: 9781457167003