Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Regular Expressions
  4. My co-worker came up with a regex that validate dates... and it even validates leap years!

My co-worker came up with a regex that validate dates... and it even validates leap years!

Scheduled Pinned Locked Moved Regular Expressions
csharpregexhelplounge
7 Posts 4 Posters 26 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sentenryu
    wrote on last edited by
    #1

    please, tell me this has a bug:

    "^(((0[1-9]|[12]\\d|3[01])\\/(0[13578]|1[02])\\/((19|[2-9]\\d)\\d{2}))|((0[1-9]|[12]\\d|30)\\/(0[13456789]|1[012])\\/((19|[2-9]\\d)\\d{2}))|((0[1-9]|1\\d|2[0-8])\\/02\\/((19|[2-9]\\d)\\d{2}))|(29\\/02\\/((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$"

    I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p)

    T 1 Reply Last reply
    0
    • S Sentenryu

      please, tell me this has a bug:

      "^(((0[1-9]|[12]\\d|3[01])\\/(0[13578]|1[02])\\/((19|[2-9]\\d)\\d{2}))|((0[1-9]|[12]\\d|30)\\/(0[13456789]|1[012])\\/((19|[2-9]\\d)\\d{2}))|((0[1-9]|1\\d|2[0-8])\\/02\\/((19|[2-9]\\d)\\d{2}))|(29\\/02\\/((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$"

      I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p)

      T Offline
      T Offline
      Thomas Daniels
      wrote on last edited by
      #2

      That's a very long expression. This is a shorter expression that validate dates:

      ^\d{4}[- /.](0?[1-9]|1[0-2])[- /.](0?[1-9]|[12][1-9]|3[01])$

      Expresso[^] is a good tool to create regular expressions.

      In some cases, my signature will be longer than my message...

      <em style="color:red"> <b>ProgramFOX</b></em>

      ProgramFOX

      S 1 Reply Last reply
      0
      • T Thomas Daniels

        That's a very long expression. This is a shorter expression that validate dates:

        ^\d{4}[- /.](0?[1-9]|1[0-2])[- /.](0?[1-9]|[12][1-9]|3[01])$

        Expresso[^] is a good tool to create regular expressions.

        In some cases, my signature will be longer than my message...

        <em style="color:red"> <b>ProgramFOX</b></em>

        ProgramFOX

        S Offline
        S Offline
        Sentenryu
        wrote on last edited by
        #3

        thanks for the reply, we actually found a bug on that expression when entering 01/01/2013 (dd/MM/yyyy), for some reason, this value was rejected...

        I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p)

        T L 2 Replies Last reply
        0
        • S Sentenryu

          thanks for the reply, we actually found a bug on that expression when entering 01/01/2013 (dd/MM/yyyy), for some reason, this value was rejected...

          I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p)

          T Offline
          T Offline
          Thomas Daniels
          wrote on last edited by
          #4

          Sentenryu wrote:

          thanks for the reply

          You're welcome!

          Sentenryu wrote:

          dd/MM/yyyy

          My expression is for yyyy/MM/dd. An expression for dd/MM/yyyy:

          ^(0?[1-9]|[12][1-9]|3[01])[- /.](0?[1-9]|1[0-2])[- /.]\d{4}$

          In some cases, my signature will be longer than my message...

          <em style="color:red"> <b>ProgramFOX</b></em>

          ProgramFOX

          P 1 Reply Last reply
          0
          • T Thomas Daniels

            Sentenryu wrote:

            thanks for the reply

            You're welcome!

            Sentenryu wrote:

            dd/MM/yyyy

            My expression is for yyyy/MM/dd. An expression for dd/MM/yyyy:

            ^(0?[1-9]|[12][1-9]|3[01])[- /.](0?[1-9]|1[0-2])[- /.]\d{4}$

            In some cases, my signature will be longer than my message...

            <em style="color:red"> <b>ProgramFOX</b></em>

            ProgramFOX

            P Offline
            P Offline
            pt1401
            wrote on last edited by
            #5

            Its much shorter yes, but it's overly simplistic. It says 31st Feb is a valid date :(

            T 1 Reply Last reply
            0
            • P pt1401

              Its much shorter yes, but it's overly simplistic. It says 31st Feb is a valid date :(

              T Offline
              T Offline
              Thomas Daniels
              wrote on last edited by
              #6

              There's a difference between a valid date and a valid date format. A regex is good for checking formats, but I don't think it works for checking whether a day exists.

              In some cases, my signature will be longer than my message...

              <em style="color:red"> <b>ProgramFOX</b></em>

              ProgramFOX

              1 Reply Last reply
              0
              • S Sentenryu

                thanks for the reply, we actually found a bug on that expression when entering 01/01/2013 (dd/MM/yyyy), for some reason, this value was rejected...

                I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p)

                L Offline
                L Offline
                Lutoslaw
                wrote on last edited by
                #7

                Maybe it checks if you have to go to work too. AFAIK 1.1 is work-free.

                Greetings - Jacek

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups