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. C#
  4. Regular Expressions

Regular Expressions

Scheduled Pinned Locked Moved C#
regexhelpquestion
8 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.
  • U Offline
    U Offline
    User 804865
    wrote on last edited by
    #1

    I am using the RegEx which needs to check for the value to be five digits before the decimal and 2 after the decimal or just five digits. and I am using System.Text.RegularExpressions.Regex exp = new System.Text.RegularExpressions.Regex("(^\\d{0,5}.{1}\\d{1,2}$)|(^\\d{1,5}$)"); With this it's only checking if the value is five digits before decimal and 2 after but not for just 5 digits. I entered 6 digits without decimal and it did not catch the exception. I don't know what's wrong. Any help ?

    K H E 3 Replies Last reply
    0
    • U User 804865

      I am using the RegEx which needs to check for the value to be five digits before the decimal and 2 after the decimal or just five digits. and I am using System.Text.RegularExpressions.Regex exp = new System.Text.RegularExpressions.Regex("(^\\d{0,5}.{1}\\d{1,2}$)|(^\\d{1,5}$)"); With this it's only checking if the value is five digits before decimal and 2 after but not for just 5 digits. I entered 6 digits without decimal and it did not catch the exception. I don't know what's wrong. Any help ?

      K Offline
      K Offline
      Kentamanos
      wrote on last edited by
      #2

      In most regular expression syntaxes, "." is a symbol that means "any character". You probably need to escape the . with a backslash, like "\.".


      I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
      -David St. Hubbins

      1 Reply Last reply
      0
      • U User 804865

        I am using the RegEx which needs to check for the value to be five digits before the decimal and 2 after the decimal or just five digits. and I am using System.Text.RegularExpressions.Regex exp = new System.Text.RegularExpressions.Regex("(^\\d{0,5}.{1}\\d{1,2}$)|(^\\d{1,5}$)"); With this it's only checking if the value is five digits before decimal and 2 after but not for just 5 digits. I entered 6 digits without decimal and it did not catch the exception. I don't know what's wrong. Any help ?

        H Offline
        H Offline
        Heath Stewart
        wrote on last edited by
        #3

        ^\d{5}(\.\d{2})?$ You didn't escape ".". Without escaping, this means that it matches any character or none. For instance, to match any 5 characters, you could use .{5}.

        -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

        U K 2 Replies Last reply
        0
        • U User 804865

          I am using the RegEx which needs to check for the value to be five digits before the decimal and 2 after the decimal or just five digits. and I am using System.Text.RegularExpressions.Regex exp = new System.Text.RegularExpressions.Regex("(^\\d{0,5}.{1}\\d{1,2}$)|(^\\d{1,5}$)"); With this it's only checking if the value is five digits before decimal and 2 after but not for just 5 digits. I entered 6 digits without decimal and it did not catch the exception. I don't know what's wrong. Any help ?

          E Offline
          E Offline
          Eric Gunnerson msft
          wrote on last edited by
          #4

          I think I would write this as: @"^\d{5}(\.\d{2})?$" Well, actually, I'd write it as: @"^ \d{5} # 5 digits ( \. # decimal point \d{2} # 2 digits )? # match zero or one time " and then use RegexOptions.IgnorePatternWhitespace (it wasn't clear to me if you meant "only 5 digits" or "from 1 to 5 digits" in your description, so you might need to change the first part.

          U 1 Reply Last reply
          0
          • H Heath Stewart

            ^\d{5}(\.\d{2})?$ You didn't escape ".". Without escaping, this means that it matches any character or none. For instance, to match any 5 characters, you could use .{5}.

            -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

            U Offline
            U Offline
            User 804865
            wrote on last edited by
            #5

            Thanks for the help It works.:laugh:

            1 Reply Last reply
            0
            • E Eric Gunnerson msft

              I think I would write this as: @"^\d{5}(\.\d{2})?$" Well, actually, I'd write it as: @"^ \d{5} # 5 digits ( \. # decimal point \d{2} # 2 digits )? # match zero or one time " and then use RegexOptions.IgnorePatternWhitespace (it wasn't clear to me if you meant "only 5 digits" or "from 1 to 5 digits" in your description, so you might need to change the first part.

              U Offline
              U Offline
              User 804865
              wrote on last edited by
              #6

              I have changed my code and it works. Actually it supposed to be 1 to 5 digits, so I changed the first part to \d{1,5}

              1 Reply Last reply
              0
              • H Heath Stewart

                ^\d{5}(\.\d{2})?$ You didn't escape ".". Without escaping, this means that it matches any character or none. For instance, to match any 5 characters, you could use .{5}.

                -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

                K Offline
                K Offline
                Kentamanos
                wrote on last edited by
                #7

                I'm pretty sure you know this and just had a brain fart while typing, but for anyone else reading this... . actually means any character except possibly "\n" depending on options you set for your regular expressions. In other words, it doesn't match "none". More details here.


                I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
                -David St. Hubbins

                H 1 Reply Last reply
                0
                • K Kentamanos

                  I'm pretty sure you know this and just had a brain fart while typing, but for anyone else reading this... . actually means any character except possibly "\n" depending on options you set for your regular expressions. In other words, it doesn't match "none". More details here.


                  I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
                  -David St. Hubbins

                  H Offline
                  H Offline
                  Heath Stewart
                  wrote on last edited by
                  #8

                  You're right - brain fart! :) I'm so used to typing .* for so many things I practically think of it as a single pattern element! ;P Thanks for the catch!

                  -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

                  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