I have created a program which takes a string (e.g "[2*4]x + [3/2]x"), isolates all the instances where there is text within square brackets and places them within an array 'matches'. It then strips off the square brackets and by some function (i am using the library flee) takes each string (e.g 2*4) and evaluates it before storing it in an array 'answers'. I now need a way to replace the items within square brackets in the original string with the items in the 'answers' array but I am not sure how to do this
public string result(string solution,int num1, int num2, int num3,int num4)
{
Regex regex = new Regex(@"\[.*?\]");
MatchCollection matches = regex.Matches(solution);
int count = matches.Count;
int [] answers = new int [10];
for (int i = 0; i <= count; i++)
{
string match = matches[i].Value;
match = match.Replace("[", "");
match = match.Replace("]", "");
Console.WriteLine(match);
ExpressionOptions options = new ExpressionOptions();
options.Imports.AddType(typeof(System.Math));
ExpressionOwner owner = new ExpressionOwner();
owner.a = num1;
owner.b = num2;
owner.c = num3;
owner.d = num4;
Expression expressionmethod = new Expression(match, owner, options);
try
{
ExpressionEvaluator<int> evaluator = (ExpressionEvaluator<int>)expressionmethod.Evaluator;
int result = evaluator();
answers[i] = result;
}
catch
{
ExpressionEvaluator<double> evaluator = (ExpressionEvaluator<double>)expressionmethod.Evaluator;
double result = evaluator();
answers[i] = Convert.ToInt32(result);
}
}
}