P
P
Pavel Yakovlev2015-12-02 08:37:44
C++ / C#
Pavel Yakovlev, 2015-12-02 08:37:44

How to parse a string in c# like it is done with command line arguments?

It is planned to read a text file line by line.
The strings will be of the same form as the arguments that are passed to the application from the command line (example):
string1 "string2" string3 string4 "string5"
the String.Split method is not suitable, because there may be spaces in the line that will be unnecessary to separate, such lines are highlighted with quotes.
Perhaps there is some kind of native function?
UPD. Found the following solution:

Regex RegExObj = new Regex("\"([^\"]+)\"|([^\\s]+)");
string[] str = File.ReadAllLines(path);
foreach (string s in str)
{
  Match matchResults = RegExObj.Match(s);
  List<string> result = new List<string>();
  while (matchResults.Success)
  {
    if (matchResults.Groups[1].Value != "")
      result.Add(matchResults.Groups[1].Value);
    else
      result.Add(matchResults.Groups[2].Value);
    matchResults = matchResults.NextMatch();
  }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
AtomKrieg, 2015-12-02
@Seinen

stackoverflow.com/questions/1189416/c-regular-expr...

M
MonkAlex, 2015-12-02
@MonkAlex

You can google exactly the command line parsers, since you need the same format.
They are different in general, so it all depends on which ones you use.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question