Regular expression for matching dd / mm / yy dates and validating values

Does anyone have an accessible regurlar expression that accepts only dates in dd / mm / yy format but also has a strict check to make sure the date is valid, including support for a leap year?

I code in vb.net and try my best to work with it.

+3
source share
8 answers

I don't think leap year support is feasible in regex without using some ugly regex.

You will need to check the date after checking the input with a regular expression.

Keeper, DateTime.ParseExact :

Public Function IsValidDate(ByVal dateString As String) As Boolean
    Try
        DateTime.ParseExact(dateString, "dd/MM/yy", System.Globalization.CultureInfo.InvariantCulture)
        Return True
    Catch ex As FormatException
        Return False
    End Try
End Function
+4

, , , , , YY . 00 , 400. 2000 , 1900 .

, 01 31, 01 12, 1900 2099 . (?:19|20), dd/mm/yy: 00 99. - ( - , ) .

^(0[1-9]|[12]\d|3[01])/(0[1-9]|1[0-2])/((?:19|20)\d{2})$
+3

, , , .

Regex.

+2
+2

, , reqular. , . :

, , , , . script, , , . , 29 . , 4 ( 100, 400) , .

+2

, .

+1

You are trying to use a regular expression hammer to solve a problem other than a nail.

Isn't it better to extract numbers with regular expressions, but check them programmatically?

0
source

There is no need to check the format, because the parsing methods will do it for you. parsing will compare all the date format strings in DateTimeFormatInfowith the string passed to the method. The exact parse method will only compare the specified string with the data format strings you pass to the method.

Imports System.Globalization

Module Sample

    Public Function IsValidDateString1(ByVal s As String) As Boolean
        Return Date.TryParseExact(s, "dd/MM/yy", CultureInfo.InvariantCulture, DateTimeStyles.None, Nothing)
    End Function

    Public Function IsValidDateString2(ByVal s As String) As Boolean
        Static _dateFormats() As String = New String() {"dd/MM/yy", "d/M/yy", "d/M/yyyy"}
        Return Date.TryParseExact(s, _dateFormats, CultureInfo.InvariantCulture, DateTimeStyles.None, Nothing)
    End Function

    Public Sub Main()
        Debug.WriteLine("single")
        Debug.WriteLine(IsValidDateString1("31/12/2001")) 'wrong format
        Debug.WriteLine(IsValidDateString1("31/12/01"))
        Debug.WriteLine(IsValidDateString1("29/2/08"))  '<-be careful
        Debug.WriteLine(IsValidDateString1("29/ 2/08")) '<-be careful
        Debug.WriteLine(IsValidDateString1("29/02/08"))
        Debug.WriteLine(IsValidDateString1("29/02/09")) 'invalide date
        Debug.WriteLine("multiple")
        Debug.WriteLine(IsValidDateString2("31/12/2001"))
        Debug.WriteLine(IsValidDateString2("31/12/01"))
        Debug.WriteLine(IsValidDateString2("29/2/08"))
        Debug.WriteLine(IsValidDateString2("29/ 2/08")) '<-be careful
        Debug.WriteLine(IsValidDateString2("29/02/08"))
        Debug.WriteLine(IsValidDateString2("29/02/09")) 'invalid date
    End Sub

End Module
0
source

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


All Articles