title: Regular Expressions - Matching IRC-like parameters?
body: <p>I am looking to create a IRC-like command format:</p>
  
  <pre><code>/commandname parameter1 "parameter 2" "parameter \"3\"" parameter"4 parameter\"5
  </code></pre>
  
  <p>Which would (ideally) give me a list of parameters:</p>
  
  <pre><code>parameter1
  parameter 2
  parameter "3"
  parameter"4
  parameter\"5
  </code></pre>
  
  <p>Now from what I have read, this isn't at all trivial and might as well be done in some other method.</p>
  
  <p>Thoughts?  </p>
  
  <p>Below is C# code that does the job I need:</p>
  
  <pre><code>public List&lt;string&gt; ParseIrcCommand(string command)
      {
          command = command.Trim();
          command = command.TrimStart(new char[] { '/' });
          command += ' ';
  
          List&lt;string&gt; Tokens = new List&lt;string&gt;();
  
          int tokenStart = 0;
          bool inQuotes = false;
          bool inToken = true;
          string currentToken = "";
          for (int i = tokenStart; i &lt; command.Length; i++)
          {
              char currentChar = command[i];
              char nextChar = (i + 1 &gt;= command.Length ? ' ' : command[i + 1]);
  
              if (!inQuotes &amp;&amp; inToken &amp;&amp; currentChar == ' ')
              {
                  Tokens.Add(currentToken);
                  currentToken = "";
                  inToken = false;
                  continue;
              }
  
              if (inQuotes &amp;&amp; inToken &amp;&amp; currentChar == '"')
              {
                  Tokens.Add(currentToken);
                  currentToken = "";
                  inQuotes = false;
                  inToken = false;
                  if (nextChar == ' ') i++;
                  continue;
              }
  
              if (inQuotes &amp;&amp; inToken &amp;&amp; currentChar == '\\' &amp;&amp; nextChar == '"')
              {
                  i++;
                  currentToken += nextChar;
                  continue;
              }
  
              if (!inToken &amp;&amp; currentChar != ' ')
              {
                  inToken = true;
                  tokenStart = i;
                  if (currentChar == '"')
                  {
                      tokenStart++;
                      inQuotes = true;
                      continue;
                  }
              }
  
              currentToken += currentChar;
          }
  
          return Tokens;
      }
  </code></pre>