Regular Expression to match valid dates

后端 未结 15 1840
庸人自扰
庸人自扰 2020-11-22 04:48

I\'m trying to write a regular expression that validates a date. The regex needs to match the following

  • M/D/YYYY
  • MM/DD/YYYY
  • Single digit mon
相关标签:
15条回答
  • 2020-11-22 04:58

    This is not an appropriate use of regular expressions. You'd be better off using

    [0-9]{2}/[0-9]{2}/[0-9]{4}
    

    and then checking ranges in a higher-level language.

    0 讨论(0)
  • 2020-11-22 04:58

    Sounds like you're overextending regex for this purpose. What I would do is use a regex to match a few date formats and then use a separate function to validate the values of the date fields so extracted.

    0 讨论(0)
  • 2020-11-22 05:00
        var dtRegex = new RegExp(/[1-9\-]{4}[0-9\-]{2}[0-9\-]{2}/);
        if(dtRegex.test(date) == true){
            var evalDate = date.split('-');
            if(evalDate[0] != '0000' && evalDate[1] != '00' && evalDate[2] != '00'){
                return true;
            }
        }
    
    0 讨论(0)
  • 2020-11-22 05:01

    Regex was not meant to validate number ranges(this number must be from 1 to 5 when the number preceding it happens to be a 2 and the number preceding that happens to be below 6). Just look for the pattern of placement of numbers in regex. If you need to validate is qualities of a date, put it in a date object js/c#/vb, and interogate the numbers there.

    0 讨论(0)
  • 2020-11-22 05:04

    Perl expanded version

    Note use of /x modifier.

    /^(
          (
            ( # 31 day months
                (0[13578])
              | ([13578])
              | (1[02])
            )
            [\/]
            (
                ([1-9])
              | ([0-2][0-9])
              | (3[01])
            )
          )
        | (
            ( # 30 day months
                (0[469])
              | ([469])
              | (11)
            )
            [\/]
            (
                ([1-9])
              | ([0-2][0-9])
              | (30)
            )
          )
        | ( # 29 day month (Feb)
            (2|02)
            [\/]
            (
                ([1-9])
              | ([0-2][0-9])
            )
          )
        )
        [\/]
        # year
        \d{4}$
      
      | ^\d{4}$ # year only
    /x
    

    Original

    ^((((0[13578])|([13578])|(1[02]))[\/](([1-9])|([0-2][0-9])|(3[01])))|(((0[469])|([469])|(11))[\/](([1-9])|([0-2][0-9])|(30)))|((2|02)[\/](([1-9])|([0-2][0-9]))))[\/]\d{4}$|^\d{4}$
    
    0 讨论(0)
  • 2020-11-22 05:04

    Perl 6 version

    rx{
      ^
    
      $<month> = (\d ** 1..2)
      { $<month> <= 12 or fail }
    
      '/'
    
      $<day> = (\d ** 1..2)
      {
        given( +$<month> ){
          when 1|3|5|7|8|10|12 {
            $<day> <= 31 or fail
          }
          when 4|6|9|11 {
            $<day> <= 30 or fail
          }
          when 2 {
            $<day> <= 29 or fail
          }
          default { fail }
        }
      }
    
      '/'
    
      $<year> = (\d ** 4)
    
      $
    }
    

    After you use this to check the input the values are available in $/ or individually as $<month>, $<day>, $<year>. ( those are just syntax for accessing values in $/ )

    No attempt has been made to check the year, or that it doesn't match the 29th of Feburary on non leap years.

    0 讨论(0)
提交回复
热议问题