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. .NET (Core and Framework)
  4. Regexp for date?

Regexp for date?

Scheduled Pinned Locked Moved .NET (Core and Framework)
regexquestioncsharpphphtml
7 Posts 4 Posters 0 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.
  • M Offline
    M Offline
    Michael Pauli
    wrote on last edited by
    #1

    Hi! I'm relativ new to RegExp and writing patterns. I wan't to write the pattern for (danish) dates between 01-01-1970 and 31-12-2099. How do I do that? I'm a little confused with the | op. I think. My best try is: string strPattern = @"^([0-2][1-9]|[3][0-1]){1}-{1}([0-1][1-2]){1}-{1}([1][9][7-9][0-9]|[2][0][0-9][0-9]){1}$"; Where (...){1} should be exactly 1 of the exclusive choices in (...) It seems to work validating it here: http://www.sweeting.org/mark/html/revalid.php#calc[^] But in c# it do not work! Whay is that? Regex rx = new Regex(strPattern); rx.IsMatch(test) - seems to be false most of the time! I can't figure it out - help!

    Michael Mogensen

    M A G P 4 Replies Last reply
    0
    • M Michael Pauli

      Hi! I'm relativ new to RegExp and writing patterns. I wan't to write the pattern for (danish) dates between 01-01-1970 and 31-12-2099. How do I do that? I'm a little confused with the | op. I think. My best try is: string strPattern = @"^([0-2][1-9]|[3][0-1]){1}-{1}([0-1][1-2]){1}-{1}([1][9][7-9][0-9]|[2][0][0-9][0-9]){1}$"; Where (...){1} should be exactly 1 of the exclusive choices in (...) It seems to work validating it here: http://www.sweeting.org/mark/html/revalid.php#calc[^] But in c# it do not work! Whay is that? Regex rx = new Regex(strPattern); rx.IsMatch(test) - seems to be false most of the time! I can't figure it out - help!

      Michael Mogensen

      M Offline
      M Offline
      Michael Pauli
      wrote on last edited by
      #2

      Should the - be enclosed in \\ like: \-\ ?

      Michael Mogensen

      1 Reply Last reply
      0
      • M Michael Pauli

        Hi! I'm relativ new to RegExp and writing patterns. I wan't to write the pattern for (danish) dates between 01-01-1970 and 31-12-2099. How do I do that? I'm a little confused with the | op. I think. My best try is: string strPattern = @"^([0-2][1-9]|[3][0-1]){1}-{1}([0-1][1-2]){1}-{1}([1][9][7-9][0-9]|[2][0][0-9][0-9]){1}$"; Where (...){1} should be exactly 1 of the exclusive choices in (...) It seems to work validating it here: http://www.sweeting.org/mark/html/revalid.php#calc[^] But in c# it do not work! Whay is that? Regex rx = new Regex(strPattern); rx.IsMatch(test) - seems to be false most of the time! I can't figure it out - help!

        Michael Mogensen

        A Offline
        A Offline
        annathor
        wrote on last edited by
        #3

        the regex works fine in Expresso, and since Expresso uses .net regex engine, it should work fine, Maybe the problem is in an another part of you code? 01-01-1970 <- Validates 31-12-2099 <- Validates 31-12-2100 <- Doesn't validate 01-01-1969 <- Doesn't validate

        M 1 Reply Last reply
        0
        • A annathor

          the regex works fine in Expresso, and since Expresso uses .net regex engine, it should work fine, Maybe the problem is in an another part of you code? 01-01-1970 <- Validates 31-12-2099 <- Validates 31-12-2100 <- Doesn't validate 01-01-1969 <- Doesn't validate

          M Offline
          M Offline
          Michael Pauli
          wrote on last edited by
          #4

          Thanx' I'll check it out...

          Michael Mogensen

          1 Reply Last reply
          0
          • M Michael Pauli

            Hi! I'm relativ new to RegExp and writing patterns. I wan't to write the pattern for (danish) dates between 01-01-1970 and 31-12-2099. How do I do that? I'm a little confused with the | op. I think. My best try is: string strPattern = @"^([0-2][1-9]|[3][0-1]){1}-{1}([0-1][1-2]){1}-{1}([1][9][7-9][0-9]|[2][0][0-9][0-9]){1}$"; Where (...){1} should be exactly 1 of the exclusive choices in (...) It seems to work validating it here: http://www.sweeting.org/mark/html/revalid.php#calc[^] But in c# it do not work! Whay is that? Regex rx = new Regex(strPattern); rx.IsMatch(test) - seems to be false most of the time! I can't figure it out - help!

            Michael Mogensen

            G Offline
            G Offline
            Gideon Engelberth
            wrote on last edited by
            #5

            {1} is entirely redundant. Anything without a specifier is required exactly once. Also, a character class with one element is just extra typing, so [1][9] should just be 19. The only obvious problem I see is the month section only gets 01, 02, 11, and 12. I would try this: ^([0-2][1-9]|3[0-1])-(0[1-9]|1[0-2])-(19[7-9][0-9]|20[0-9][0-9])$

            M 1 Reply Last reply
            0
            • G Gideon Engelberth

              {1} is entirely redundant. Anything without a specifier is required exactly once. Also, a character class with one element is just extra typing, so [1][9] should just be 19. The only obvious problem I see is the month section only gets 01, 02, 11, and 12. I would try this: ^([0-2][1-9]|3[0-1])-(0[1-9]|1[0-2])-(19[7-9][0-9]|20[0-9][0-9])$

              M Offline
              M Offline
              Michael Pauli
              wrote on last edited by
              #6

              Also this works: private readonly string STR_DDMMYYYY_PATTERN = @"^([0][1-9]|[1-2][0-9]|[3][0-1])-([0][1-9]|[1][0-2])-([1][9][7-9][0-9]|[2][0][0-9][0-9])$"; I figured the {1} problem out also. Seems that the validator-site is OK with dealing with apparently redundant chars - but class Regex are not? So now this is OK:

              Regex regexpDDMMYYYY = new Regex(STR_DDMMYYYY_PATTERN);
              bool bIsValid = regexpDDMMYYYY.IsMatch(tbGeneral.Text);
              if (bIsValid)
              {
              ...
              etc.

              I'll test the [1][9] replacement also. Thanx' again.

              Michael Mogensen

              1 Reply Last reply
              0
              • M Michael Pauli

                Hi! I'm relativ new to RegExp and writing patterns. I wan't to write the pattern for (danish) dates between 01-01-1970 and 31-12-2099. How do I do that? I'm a little confused with the | op. I think. My best try is: string strPattern = @"^([0-2][1-9]|[3][0-1]){1}-{1}([0-1][1-2]){1}-{1}([1][9][7-9][0-9]|[2][0][0-9][0-9]){1}$"; Where (...){1} should be exactly 1 of the exclusive choices in (...) It seems to work validating it here: http://www.sweeting.org/mark/html/revalid.php#calc[^] But in c# it do not work! Whay is that? Regex rx = new Regex(strPattern); rx.IsMatch(test) - seems to be false most of the time! I can't figure it out - help!

                Michael Mogensen

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                A while back, I wrote a blog entry[^] on a general purpose regular expression date validator that you might want to take a look at. The code should give you some ideas, especially as it can show you why regular expressions for dates aren't enough on their own.

                "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                My blog | My articles | MoXAML PowerToys | Onyx

                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