Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9255793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:47:24+00:00 2026-06-18T11:47:24+00:00

I am looking to create a IRC-like command format: /commandname parameter1 parameter 2 parameter

  • 0

I am looking to create a IRC-like command format:

/commandname parameter1 "parameter 2" "parameter \"3\"" parameter"4 parameter\"5

Which would (ideally) give me a list of parameters:

parameter1
parameter 2
parameter "3"
parameter"4
parameter\"5

Now from what I have read, this isn’t at all trivial and might as well be done in some other method.

Thoughts?

Below is C# code that does the job I need:

public List<string> ParseIrcCommand(string command)
    {
        command = command.Trim();
        command = command.TrimStart(new char[] { '/' });
        command += ' ';

        List<string> Tokens = new List<string>();

        int tokenStart = 0;
        bool inQuotes = false;
        bool inToken = true;
        string currentToken = "";
        for (int i = tokenStart; i < command.Length; i++)
        {
            char currentChar = command[i];
            char nextChar = (i + 1 >= command.Length ? ' ' : command[i + 1]);

            if (!inQuotes && inToken && currentChar == ' ')
            {
                Tokens.Add(currentToken);
                currentToken = "";
                inToken = false;
                continue;
            }

            if (inQuotes && inToken && currentChar == '"')
            {
                Tokens.Add(currentToken);
                currentToken = "";
                inQuotes = false;
                inToken = false;
                if (nextChar == ' ') i++;
                continue;
            }

            if (inQuotes && inToken && currentChar == '\\' && nextChar == '"')
            {
                i++;
                currentToken += nextChar;
                continue;
            }

            if (!inToken && currentChar != ' ')
            {
                inToken = true;
                tokenStart = i;
                if (currentChar == '"')
                {
                    tokenStart++;
                    inQuotes = true;
                    continue;
                }
            }

            currentToken += currentChar;
        }

        return Tokens;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:47:25+00:00Added an answer on June 18, 2026 at 11:47 am

    You have shown your code – that’s good, but it seems that you haven’t thought about whether it is reasonable to parse the command like that:

    • Firstly, your code will allow new line character inside the command name and parameters. It would be reasonable if you assume that new line character can never be there.
    • Secondly, \ also needs to be escaped like ", since there will be no way to specify a single \ at the end of a parameter without causing any confusion.
    • Thirdly, it is a bit weird to have the command name parsed the same way as parameters – command names are usually per-determined and fixed, so there is no need to allow for flexible ways to specify it.

    I cannot think of one-line solution in JavaScript that is general. JavaScript regex lacks \G, which asserts the last match boundary. So my solution will have to make do with beginning of string assertion ^ and chomping off the string as a token is matched.

    (There is not much code here, mostly comments)

    function parseCommand(str) {
        /*
         * Trim() in C# will trim off all whitespace characters
         * \s in JavaScript regex also match any whitespace character
         * However, the set of characters considered as whitespace might not be
         * equivalent
         * But you can be sure that \r, \n, \t, space (ASCII 32) are included.
         * 
         * However, allowing all those whitespace characters in the command
         * is questionable.
         */
        str = str.replace(/^\s*\//, "");
    
        /* Look-ahead (?!") is needed to prevent matching of quoted parameter with
         * missing closing quote
         * The look-ahead comes from the fact that your code does not backtrack
         * while the regex engine will backtrack. Possessive qualifier can prevent
         * backtracking, but it is not supported by JavaScript RegExp.
         *
         * We emulate the effect of \G by using ^ and repeatedly chomping off
         * the string.
         *
         * The regex will match 2 cases:
         * (?!")([^ ]+)
         * This will match non-quoted tokens, which are not allowed to 
         * contain spaces
         * The token is captured into capturing group 1
         *
         * "((?:[^\\"]|\\[\\"])*)"
         * This will match quoted tokens, which consists of 0 or more:
         * non-quote-or-backslash [^\\"] OR escaped quote \"
         * OR escaped backslash \\
         * The text inside the quote is captured into capturing group 2
         */
        var regex = /^ *(?:(?!")([^ ]+)|"((?:[^\\"]|\\[\\"])*)")/;
        var tokens = [];
        var arr;
    
        while ((arr = str.match(regex)) !== null) {
            if (arr[1] !== void 0) {
                // Non-space token
                tokens.push(arr[1]);
            } else {
                // Quoted token, needs extra processing to
                // convert escaped character back
                tokens.push(arr[2].replace(/\\([\\"])/g, '$1'));
            }
    
            // Remove the matched text
            str = str.substring(arr[0].length);
        }
    
        // Test that the leftover consists of only space characters
        if (/^ *$/.test(str)) {
            return tokens;
        } else {
            // The only way to reach here is opened quoted token
            // Your code returns the tokens successfully parsed
            // but I think it is better to show an error here.
            return null;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im looking to create a control that would look like comic baloon. In WPF
I'm looking to create something like in this image. Each block would have an
Im looking to create a conditional within an in_array statement. Basically, I would like
I am looking to create a plugin manager like program which starts a loop
Looking to create a list like this: 1. Section Header 1.1 Some Text Here
Looking to create similar effect to http://www.youtube.com/create/GoAnimate I like the cartoon avatar which guide
I'm looking to create a command line menu in powershell, with the options coming
I'm looking to create a windows program that doesn't require any environmental dependencies (like
Looking to create an inline array in Visual Basic for Applications Something like this
Im looking to create a form where the user will select from a list

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.