4

I have a string with quotes around the path as follows:

"C:\Program Files (x86)\Windows Media Player\wmplayer.exe" arg1 arg2

If I use Text.Split(new Char[] { ' ' }, 2); then I get the first space.

How to get the path and args ?

Kaya
  • 515
  • 2
  • 7
  • 19

3 Answers3

4

Use a regular expression like: ("".*?"")|(\S+)

So your code would be something like:

Regex r = new Regex(@"("".*?"")|(\S+)"); 
MatchCollection mc = r.Matches(input);
for (int i = 0; i < mc.Count; i++) 
{
   Console.WriteLine(mc[i].Value);
}
James Schek
  • 17,844
  • 7
  • 51
  • 64
2

Try splitting on the double quotes (Text.Split(new Char[] { '/"' }, 3);) then taking the last string in that array and splitting again on the space.

string[] pathAndArgs = Text.Split(new Char[] { '/"' }, 3);
string[] args = pathAndArgs[2].Split(new Char[] { ' ' }, 2);

I may have a syntax error in there, but you get what I mean.

Matthew Jones
  • 25,644
  • 17
  • 102
  • 155
  • Cheers I went with this string[] fileAndArgs = this.AppPath.Text.Split(new Char[] { '"' }, 3); – Kaya Jun 26 '09 at 14:35
  • 1
    This wouldn't work if the arguments are themselves double quoted to enable them to include spaces. (Not sure if that is the case here: original question does not clarify.) – peSHIr Jun 26 '09 at 14:52
1

Do text.split and work your way back from the end of the array.

var input = "C:\\blah\\win.exe args1 args2";
var array = input.split(' ');
var arg1 = array[array.length -2];
var arg2 = array[array.length -1];
kemiller2002
  • 113,795
  • 27
  • 197
  • 251
  • This wouldn't work if the arguments are themselves double quoted to enable them to include spaces. (Not sure if that is the case here: original question does not clarify.) – peSHIr Jun 26 '09 at 14:51