Answer the question
In order to leave comments, you need to log in
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question