P
P
pavviaz2017-10-18 20:45:53
Regular Expressions
pavviaz, 2017-10-18 20:45:53

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);

Now you need to somehow convert matches to an array of strings or something similar, where it will be convenient to process the found matches. I can't figure out how to convert MatchCollection to string[] (or something else). Help me please. Thanks :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Nemiro, 2017-10-18
@pavviaz

Or loop through and collect an array / collection:

var result = new List<string>();

foreach (Match m in matches)
{
  result.Add(m.Value);
}

// в result будет коллекция строк

Or you can use System.Linq :
Somehow showed an example of a solver , but in VB.NET . If desired, you can do the same in C# .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question