Regex problem while filtering for decimal


Jun 5, 2018
Programming Experience
I have a string entry as follows: inputString="2;2" which I match against a regular expression.

Regex regex = new Regex(@"\b[0-9]*\.*[0-9]+$");
Match match = regex.Match(inputString);

The problem is: it seems to match inspite of the fact that "2;2" ought not to match since it is definitely not a decimal number, and I have the intention to filter for decimals. What am I doing wrong?
It matches last "2" because \b is word boundary and ";" is not a word character. * repeats zero or more times
I have refactored it like here:

//This is how to match singfle decimals w/o whitespaces.
string inputString = "23.475";
Regex regex = new Regex(@"^[0-9]*\.*[0-9]+$");
Match match = regex.Match(inputString);
//This is how to match sums w/o whitespaces.
string inputString2 = "45.475+23.3";
Regex regex2 = new Regex(@"^[0-9]*\.*[0-9]+(\+[0-9]*\.*[0-9]+)?$");
Match match2 = regex.Match(inputString);
There is still one thing, though, which is fully behind my comprehension: After some further refactoring, I’d like to match an addition with the following Regex.

Regex regex = new Regex(@"^([0-9]*\.[0-9]+)\+([0-9]*\.[0-9]+)$");  
Match match = regex.Match(inputString);
if (match.Success)
                Console.WriteLine("This is an addition.");

It apparently does not match expressions like string inputString = "45.475+23.3"; I do not understand what is wrong about the above regex.
Indeed - I somehow missed it. Now this ought to be the last refactoring:

 string inputString = "43";

            Regex regexDecimal = new Regex(@"^[0-9]*\.?[0-9]+$");
            Match matchDecimal = regexDecimal.Match(inputString);

            Regex regexAddition = new Regex(@"^([0-9]*\.?[0-9]+)\+([0-9]*\.?[0-9]+)$");
            Match matchAddition = regexAddition.Match(inputString);

            if (matchDecimal.Success)
                Console.WriteLine("This is a non decimal or a decimal.");

            if (matchAddition.Success)
                Console.WriteLine("This is an addition containing both non decimal and decimal numbers.");
Top Bottom