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 6997499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:17:27+00:00 2026-05-27T20:17:27+00:00

I want to split a string by white spaces except if the text inside

  • 0

I want to split a string by white spaces except if the text inside the string is in double quotes (“text”) or single quotes (‘text’).

I am doing it with this function:

public static string[] ParseKeywordExpression(string keywordExpressionValue, bool isUniqueKeywordReq)
{
    keywordExpressionValue = keywordExpressionValue.Trim();
    if (keywordExpressionValue == null || !(keywordExpressionValue.Length > 0))
        return new string[0];
    int idx = keywordExpressionValue.Trim().IndexOf(" ");
    if (idx == -1)
        return new string[] { keywordExpressionValue };
    //idx = idx + 1;
    int count = keywordExpressionValue.Length;
    ArrayList extractedList = new ArrayList();
    while (count > 0)
    {
        if (keywordExpressionValue[0] == '"')
        {
            int temp = keywordExpressionValue.IndexOf(BACKSLASH, 1, keywordExpressionValue.Length - 1);
            while (keywordExpressionValue[temp - 1] == '\\')
            {
                temp = keywordExpressionValue.IndexOf(BACKSLASH, temp + 1, keywordExpressionValue.Length - temp - 1);
            }
            idx = temp + 1;
        }
        if (keywordExpressionValue[0] == '\'')
        {
            int temp = keywordExpressionValue.IndexOf(BACKSHASH_QUOTE, 1, keywordExpressionValue.Length - 1);
            while (keywordExpressionValue[temp - 1] == '\\')
            {
                temp = keywordExpressionValue.IndexOf(BACKSHASH_QUOTE, temp + 1, keywordExpressionValue.Length - temp - 1);
            }
            idx = temp + 1;
        }
        string s = keywordExpressionValue.Substring(0, idx);
        int left = count - idx;
        keywordExpressionValue = keywordExpressionValue.Substring(idx, left).Trim();
        if (isUniqueKeywordReq)                    
        {
            if (!extractedList.Contains(s.Trim('"')))
            {
                extractedList.Add(s.Trim('"'));
            }
        }
        else
        {
            extractedList.Add(s.Trim('"'));
        }
        count = keywordExpressionValue.Length;
        idx = keywordExpressionValue.IndexOf(SPACE);
        if (idx == -1)
        {
            string add = keywordExpressionValue.Trim('"', ' ');
            if (add.Length > 0)
            {
                if (isUniqueKeywordReq )
                {
                    if (!extractedList.Contains(add))
                    {
                        extractedList.Add(add);
                    }
                }
                else
                {
                    extractedList.Add(add);
                }
            }                   
            break;
        }
    }
    return (string[])extractedList.ToArray(typeof(string));
}

Is there any other way to do it or can this function can be optimised?

For example, I wish to split the string

%ABC% %aasdf% aalasdjjfas “c:\Document and Setting\Program Files\abc.exe”

to

%ABC%
%aasdf%
aalasdjjfas
“c:\Document and Setting\Program Files\abc.exe”

  • 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-05-27T20:17:28+00:00Added an answer on May 27, 2026 at 8:17 pm

    The very simplest regex for this, handling single and double quotes:

    ("((\\")|([^"]))*")|('((\\')|([^']))*')|(\S+)

    var regex = new Regex(@"(""((\\"")|([^""]))*"")|('((\\')|([^']))*')|(\S+)");
    var matches = regex.Matches(inputstring);
    foreach (Match match in matches) {
        extractedList.Add(match.Value);
    }
    

    So basically four to five lines of code is enough.

    The expression, explained:

    Main structure:
    ("((\\")|([^"]))*")    Double-quoted token
    |                      , or
    ('((\\')|([^']))*')    single-quoted token
    |                      , or
    (\S+)                  any group of non-space characters
    
    Double-quoted token:
    (                      Group starts
        "                  Initial double-quote
        (                  Inner group starts
            (\\")          Either a backslash followed by a double-quote
            |              , or  
            ([^"])         any non-double-quote character
        )*                 The inner group repeats any number of times (or zero)
        "                  Ending double-quote
    )
    
    Single-quoted token:
    (                      Group starts
        '                  Initial single-quote
        (                  Inner group starts
            (\\')          Either a backslash followed by a single-quote
            |              , or  
            ([^'])         any non-single-quote character
        )*                 The inner group repeats any number of times (or zero)
        '                  Ending single-quote
    )
    
    Non-space characters:
    (                      Group starts
        \S                 Non-white-space character
        +                  , repeated at least once
    )                      Group ends
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to split a string with multiple white spaces. I only want
I want to split a string like this: abc//def//ghi into a part before and
I want to split a command line like string in single string parameters. How
I want to split a string that can look like this: word1;word2;word3,word4,word5,word6.word7. etc. The
I want to split a string into a list or array. Input: green,yellow,green,white,orange,blue,black The
I have string like this /c SomeText\MoreText Some Text\More Text\Lol SomeText I want to
I want to split a string like 001A into 001 and A
I have a string ABCD:10,20,,40;1/1;1/2,1/3,1/4 I want to split the string into the following
Really simple problem: I want to split a connection string into its keyword /
Using Java (1.6) I want to split an input string that has components of

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.