Answer the question
In order to leave comments, you need to log in
How can a MatchCollection be converted to an array of strings in c#?
Hello, I am writing a program to automatically solve equations in Visual C#. After entering the equation and some transformations, we get, for example, this:
Next, here it is necessary to bring similar terms. For this I wanted to use regular expressions. For example, to select "-x" or "+x" I use the following:
Since there can be more than one such member in the equation, I use the Regex.Match es method .
As a result, to get a variable of the MathCollection type, I wrote this:string Equation= "+5x-25x+151-x+x+325x-1+";
string Xplusandmines = @"[+--][x]";
MatchCollection matches = Regex.Matches(Equation, Xplusandmines);
Answer the question
In order to leave comments, you need to log in
Or loop through and collect an array / collection:
var result = new List<string>();
foreach (Match m in matches)
{
result.Add(m.Value);
}
// в result будет коллекция строк
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question