Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have tried to create a parse math multiplication of numbers function for example
2*8 parse to 16, with regex and regex.replace

my problem is while replacing it isn't replaced with the pattern string

What I have tried:

C#
Match match = Regex.Match(Equation, @"^(\d+\.?\d*)\*(\d+\.?\d*)");
           if (checkNULL(match) == false)
               match = Regex.Match(Equation, @"(\d+\.?\d*)\*(\d+\.?\d*)");
           double a = double.Parse(match.Groups[1].Value.ToString());
           double b = double.Parse(match.Groups[2].Value.ToString());
           Equation = Regex.Replace(Equation, $@"{match}", (a * b).ToString());
Posted
Updated 19-Jun-19 14:07pm
v3
Comments
Member 14145167 19-Jun-19 18:52pm    
when I insert @"(\d+\.?\d*)\*(\d+\.?\d*)" to Regex.Replace the function works
but it doesn't what I want
Patrice T 19-Jun-19 20:00pm    
Show sample input with actual output and expected.
BillWoodruff 20-Jun-19 17:26pm    
Is your input string always as simple as "(2*8)" ?

You're Equation does not contain the string @"{match}", therefore the replace is failing.
 
Share this answer
 
You're Equation does not contain the string "{match}", therefore the replace is failing.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900