using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; public static class Test { private static Regex qoutesRegex = new Regex("(? Split(this string str, Func controller) { int nextPiece = 0; for (int c = 0; c < str.Length; c++) { if (controller(str[c])) { yield return str.Substring(nextPiece, c - nextPiece); nextPiece = c + 1; } } yield return str.Substring(nextPiece); } public static IEnumerable SplitCommandLine(string commandLine) { bool inQuotes = false; bool isEscaping = false; return commandLine.Split(c => { if (c == '\\' && !isEscaping) { isEscaping = true; return false; } if (c == '\"' && !isEscaping) inQuotes = !inQuotes; isEscaping = false; return !inQuotes && Char.IsWhiteSpace(c)/*c == ' '*/; }) .Select(arg => arg.Trim()) .Select(arg => qoutesRegex.Replace(arg, "").Replace("\\\"", "\"")) .Where(arg => !string.IsNullOrEmpty(arg)); } public static void Main() { IEnumerable tests = new List (){ "\"He whispered to her \\\"I love you\\\".\"", "\"C:\\Program Files\"", "\"He whispered to her \\\"I love you\\\".\"", "a\" \\\"asdsd \\\"dsdsd AAA\"b", "\" \\\"asdsd \\\"dsdsd \"basds", "sdsd\" \\\"asdsd \\\"dsdsd \"", "\"A\\\"asdsd \\\"dsdsdA\"" }; foreach (var test in tests){ foreach(var s in SplitCommandLine(test)){ Console.WriteLine("Param `{0}`",s); } Console.WriteLine("-----------------"); } } }