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#
regexcsharphelptutorialquestion
12 Posts 6 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.
  • R Offline
    R Offline
    reshsilk
    wrote on last edited by
    #1

    hello, i'm using c# to create a windows app. i would like to validate the contents of a text box to make sure it is a floating point number. does anybody know how to use regular expressions to accomplish this? i've never used them before... here's what i have so far: private void startValBox_Validating(object sender, CancelEventArgs e) { if (Regex.IsMatch(startValBox.Text, "regular expression goes here")) { startVal = System.Convert.ToDouble(startValBox.Text); errorMsg.Hide(); //return true; } else { errorMsg.Text = "The start value is invalid."; errorMsg.Show(); //return false; } } thanks for your help! rc

    L P D 3 Replies Last reply
    0
    • R reshsilk

      hello, i'm using c# to create a windows app. i would like to validate the contents of a text box to make sure it is a floating point number. does anybody know how to use regular expressions to accomplish this? i've never used them before... here's what i have so far: private void startValBox_Validating(object sender, CancelEventArgs e) { if (Regex.IsMatch(startValBox.Text, "regular expression goes here")) { startVal = System.Convert.ToDouble(startValBox.Text); errorMsg.Hide(); //return true; } else { errorMsg.Text = "The start value is invalid."; errorMsg.Show(); //return false; } } thanks for your help! rc

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Are you sure you need a regex? Won't double.TryParse() do the job? regards

      R 1 Reply Last reply
      0
      • R reshsilk

        hello, i'm using c# to create a windows app. i would like to validate the contents of a text box to make sure it is a floating point number. does anybody know how to use regular expressions to accomplish this? i've never used them before... here's what i have so far: private void startValBox_Validating(object sender, CancelEventArgs e) { if (Regex.IsMatch(startValBox.Text, "regular expression goes here")) { startVal = System.Convert.ToDouble(startValBox.Text); errorMsg.Hide(); //return true; } else { errorMsg.Text = "The start value is invalid."; errorMsg.Show(); //return false; } } thanks for your help! rc

        P Offline
        P Offline
        Paul Conrad
        wrote on last edited by
        #3

        Have you looked at The 30 Minute Regex Tutorial[^]?

        1 Reply Last reply
        0
        • R reshsilk

          hello, i'm using c# to create a windows app. i would like to validate the contents of a text box to make sure it is a floating point number. does anybody know how to use regular expressions to accomplish this? i've never used them before... here's what i have so far: private void startValBox_Validating(object sender, CancelEventArgs e) { if (Regex.IsMatch(startValBox.Text, "regular expression goes here")) { startVal = System.Convert.ToDouble(startValBox.Text); errorMsg.Hide(); //return true; } else { errorMsg.Text = "The start value is invalid."; errorMsg.Show(); //return false; } } thanks for your help! rc

          D Offline
          D Offline
          Dustin Metzgar
          wrote on last edited by
          #4

          While regular expressions are a nice choice and the code looks right, why not just Double.Parse() and catch the exception?

          J 1 Reply Last reply
          0
          • D Dustin Metzgar

            While regular expressions are a nice choice and the code looks right, why not just Double.Parse() and catch the exception?

            J Offline
            J Offline
            J4amieC
            wrote on last edited by
            #5

            Controling program flow with exceptions is bad practice! Exceptions for exceptional behaviour - the user typing an invalid value is not exceptional, it's expected. In this case the framework (v2.0) provides the TryParse for exactly this scenario. Current blacklist svmilky - Extremely rude | FeRtoll - Rude personal emails | ironstrike1 - Rude & Obnoxious behaviour

            P D 2 Replies Last reply
            0
            • J J4amieC

              Controling program flow with exceptions is bad practice! Exceptions for exceptional behaviour - the user typing an invalid value is not exceptional, it's expected. In this case the framework (v2.0) provides the TryParse for exactly this scenario. Current blacklist svmilky - Extremely rude | FeRtoll - Rude personal emails | ironstrike1 - Rude & Obnoxious behaviour

              P Offline
              P Offline
              Paul Conrad
              wrote on last edited by
              #6

              Though you can use regular expressions, the TryParse would be a bit quicker. Feed it the value in the textbox and if it returns true then all is good. The regular expression would be the "show-off" route and the boss or project lead may or may not be impressed.

              1 Reply Last reply
              0
              • J J4amieC

                Controling program flow with exceptions is bad practice! Exceptions for exceptional behaviour - the user typing an invalid value is not exceptional, it's expected. In this case the framework (v2.0) provides the TryParse for exactly this scenario. Current blacklist svmilky - Extremely rude | FeRtoll - Rude personal emails | ironstrike1 - Rude & Obnoxious behaviour

                D Offline
                D Offline
                Dustin Metzgar
                wrote on last edited by
                #7

                I agree with your statement, within reason. If you want to parse a date, you don't want to have to write a regular expression for every possible accepted date format. TryParse is a nice addition indeed, but in 1.1, I'd much rather take the exception.

                J S 2 Replies Last reply
                0
                • L Lost User

                  Are you sure you need a regex? Won't double.TryParse() do the job? regards

                  R Offline
                  R Offline
                  reshsilk
                  wrote on last edited by
                  #8

                  Double.TryParse() did the trick. I'm still quite new to all this stuff, so I had no idea about that function. Definitely better than trying to use regular expressions. Thank You! rc

                  L 1 Reply Last reply
                  0
                  • D Dustin Metzgar

                    I agree with your statement, within reason. If you want to parse a date, you don't want to have to write a regular expression for every possible accepted date format. TryParse is a nice addition indeed, but in 1.1, I'd much rather take the exception.

                    J Offline
                    J Offline
                    J4amieC
                    wrote on last edited by
                    #9

                    Well, in 1.1 i'd use a RegEx for this... horses for courses! Current blacklist svmilky - Extremely rude | FeRtoll - Rude personal emails | ironstrike1 - Rude & Obnoxious behaviour

                    D 1 Reply Last reply
                    0
                    • R reshsilk

                      Double.TryParse() did the trick. I'm still quite new to all this stuff, so I had no idea about that function. Definitely better than trying to use regular expressions. Thank You! rc

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      You're welcome :)

                      1 Reply Last reply
                      0
                      • J J4amieC

                        Well, in 1.1 i'd use a RegEx for this... horses for courses! Current blacklist svmilky - Extremely rude | FeRtoll - Rude personal emails | ironstrike1 - Rude & Obnoxious behaviour

                        D Offline
                        D Offline
                        Dustin Metzgar
                        wrote on last edited by
                        #11

                        And I appreciate the comment. It's important to stress good practice on the forums.

                        1 Reply Last reply
                        0
                        • D Dustin Metzgar

                          I agree with your statement, within reason. If you want to parse a date, you don't want to have to write a regular expression for every possible accepted date format. TryParse is a nice addition indeed, but in 1.1, I'd much rather take the exception.

                          S Offline
                          S Offline
                          Stephan Samuel
                          wrote on last edited by
                          #12

                          1.1 has Double.TryParse(). If that's not good enough, this is the fastest way to determine if a string contains an integer, and it can easily be adapted to work for any other type of numeric: bool TryParseInt(string value) { for (int i = 0; i < value.Length; i++) { if (i == 0 && value[i] == '-') continue; if (!Char.IsNumber(value[i])) return false; } return value.Length < Int32.MaxValue.ToString().Length; }

                          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