I broke my whole head =) lines are read from a text file. In one line there can be "several parameters". It is necessary to divide the line into these parameters.

There are several conditions:

  • Parameters are separated by spaces.

  • The parameter may contain spaces, but in this case it must be enclosed in double quotes ("").

  • No one forbids enclosing parameters without spaces in quotes.

For example, you need to divide the following line into three.

D:\test "D:\test\Название папки" "D:\test\test" 

At the output of the function should receive 3 lines containing respectively:

 D:\test D:\test\Название папки D:\test\test 

Tell me, how would I do this? Using string.split () does not work in this case, so I decided that it needs to be implemented using regexp.match () ...

The task is further complicated by the fact that the number of parameters may not be limited. By the end of the line is meant \ r \ n

Thank you again for helping me in my task.

  • And I suppose you can escape gaps backslash? - VladD
  • one
    Is it necessary to implement using regular expressions? A hand-held scanner is probably easier and more likely to be. - VladD

2 answers 2

Try

 ".+?"|[^ ]+ 

Only it will be necessary to remove the quotes from the parameters that have them.

  • Thank you - what the doctor ordered =) - pincher1519
  • thank! just came in handy - Pavel Azanov

Here's a handheld parser (pulled from here ):

 public static string[] SplitArguments(string commandLine) { var parmChars = commandLine.ToCharArray(); var inSingleQuote = false; var inDoubleQuote = false; for (var index = 0; index < parmChars.Length; index++) { if (parmChars[index] == '"' && !inSingleQuote) { inDoubleQuote = !inDoubleQuote; parmChars[index] = '\n'; } if (parmChars[index] == '\'' && !inDoubleQuote) { inSingleQuote = !inSingleQuote; parmChars[index] = '\n'; } if (!inSingleQuote && !inDoubleQuote && parmChars[index] == ' ') parmChars[index] = '\n'; } return (new string(parmChars)).Split(new[] {'\n'}, StringSplitOptions.RemoveEmptyEntries); } 

It also supports single quotes.

You can rewrite as a generator, it will be a little more efficient.

  • Thank you, I thought about manual parsing, but this is not a beautiful solution, in which it is much easier to make a mistake or not to watch something than to use ready-made short solutions. As they say - the more lines of code - the more errors in it. @petya answered the question briefly, and what you need :-) - pincher1519
  • @ pincher1519: think about support for escaping quotes with regular expressions. It will be more difficult there. :) - VladD
  • @VladD, it was primarily about the full names of folders and files, and since the path can not contain some service characters, including quotes, then you should not think about their screening =) - pincher1519
  • @ pincher1519: it depends on which file system. On many unixes, any characters in the file name are generally allowed, except for \0 and / . - VladD
  • one
    @ pincher1519: under the Nix there is a mono , which already supports C # 5.0. - VladD