Reputation: 2280
I have a file im trying to parse that is written in this format:
command -l KEY "VALUE"
My goal is to get the KEY and the VALUE, which is inside double quotes.
Every line begins with the same command -l
. Right now, im doing this a rather inefficient way. I'm getting a substring deleting the first 11 characters, trimming out everything after the space and then getting another substring with the quoted characters.
Surely there must be a better way to do this and I think REGEX is that solution. I've been following this page here but am totally lost: https://msdn.microsoft.com/en-us/library/ms174214.aspx
Would appreciate any advice.
Upvotes: 0
Views: 91
Reputation: 21
I would use a command line parser package available from nuget (like https://www.nuget.org/packages/CommandLineParser/1.9.71)
There are plenty of them around and then you don't have to worry about handling all error or special cases.
Upvotes: 1
Reputation: 51319
For a simple string parsing exercise like this, it's very unlikely that regex will perform significantly better than manually taking a substring then calling string.Split(" ")
. For example:
public KeyValuePair<string, string> Parse(string input) {
var split = input.Substring(11).Split(' ');
return new KeyValuePair<string, string>(split[0], split[1].Replace("\"",""));
}
Upvotes: 1