I have written regexes for recognizing float and int but they don't seem to work (code below).
{
string sumstring = "12.098";
Regex flt = new Regex(@" ^[0-9]*(\.[0-9]*)");
Regex ent = new Regex("^[0-9]+");
if (d_type.IsMatch(sumstring))
{
Console.WriteLine(sumstring + " " + "dtype");
}
Match m = ent.Match(sumstring);
if (m.Success)
{
Console.WriteLine("int");
}
else if (flt.IsMatch(sumstring))
{
Console.WriteLine("float");
}
}
Where is the mistake?
First, I don't think regular expressions are really the best tool for this job. I would simply use the Double.TryParse() and Int32.TryParse() functions.
Second, you're missing a whole lot of test cases with your regular expressions:
All of these (except maybe the edge case) would be immediately covered by using the library instead of hand-rolling a regex.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With