Regular Expressions
-
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
Have you looked at The 30 Minute Regex Tutorial[^]?
-
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
While regular expressions are a nice choice and the code looks right, why not just Double.Parse() and catch the exception?
-
While regular expressions are a nice choice and the code looks right, why not just Double.Parse() and catch the exception?
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
-
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
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.
-
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
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.
-
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.
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
-
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
-
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
And I appreciate the comment. It's important to stress good practice on the forums.
-
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.
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; }