0

I'm trying to parse command line options read from an already started process. The raw string looks like that
-skipIntro -noSplash -cpuCount=7 -exThreads=7 -enableHT -malloc=tbb4malloc_bi_x64 -hugePages -maxmem=4096 -maxvram=2048 -world=empty -showScriptErrors -mod=

Single parameters can have four different formats

  • Parameter without value: -skipIntro
  • Escaped parameter without value: "-skipIntro"
  • Parameter with value: -mod=foo
  • Escaped parameter with value: "-mod=foo" or "-mod=b ar"

Escaped parameters could include white spaces.
I want to split all parameters into an array of strings. The splitted parameters should include the prefix - respectively "-.

My regex looks like this: \s*[\"]*?- which is working fine. However this also removes the prefix mentioned above. How can I include the prefix into the separated params array?

Regex101

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Christian Klemm
  • 1,455
  • 5
  • 28
  • 49

2 Answers2

1

I have a solution not splitting the string. Instead it returns matches.

(?<=^|\s)"-.*?"|-[^\s]*?(?=$|\s)

Explanation:

The general pattern (?<=prefix)find(?=suffix) is used that matches a position between a prefix and a suffix. Where

Prefix is: ^|\s beginning of string or space character.
Suffix is: $|\s end of string or space character.

Find: "-.*?"|-[^\s]*? finds either "-, followed by any characters, followed by "
OR
-, followed by any character except a space character.
Both variants with as few characters as possible (indicated by ?).

string pattern = @"(?<=^|\s)""-.*?""|-[^\s]*?(?=$|\s)";
string input = "-skipIntro \"-skipIntro\" -noSplash \"-mod=foo\" \"-mod=b ar\" -cpuCount=7";
MatchCollection matches = Regex.Matches(input, pattern);
foreach (Match match in matches) {
    Console.WriteLine(match.Value);
}

Note that in verbatim strings @"...", double quotes " are espcaped by doubling them.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
0

would that regex work for what you're trying to achieve ?

((\")?-.*?(?(2)\"|(\s|$)))

Here's a fork of your Regex101

Note that the first group captures every option, but you're gonna have to trim it as it keeps the following one whitespace that separates the options. That's the best I could do to keep the surrounding quotes.

Paul-Etienne
  • 796
  • 9
  • 23