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

  • Home
  • SEARCH
  • 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 5838009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:25:20+00:00 2026-05-22T11:25:20+00:00

Basically, I’m being passed a string and I need to tokenise it in much

  • 0

Basically, I’m being passed a string and I need to tokenise it in much the same manner as command line options are tokenised by a *nix shell

Say I have the following string

"Hello\" World" "Hello Universe" Hi

How could I turn it into a 3 element list

  • Hello” World
  • Hello Universe
  • Hi

The following is my first attempt, but it’s got a number of problems

  • It leaves the quote characters
  • It doesn’t catch the escaped quote

Code:

public void test() {
    String str = "\"Hello\\\" World\" \"Hello Universe\" Hi";
    List<String> list = split(str);
}

public static List<String> split(String str) {
    Pattern pattern = Pattern.compile(
        "\"[^\"]*\"" + /* double quoted token*/
        "|'[^']*'" + /*single quoted token*/
        "|[A-Za-z']+" /*everything else*/
    );

    List<String> opts = new ArrayList<String>();
    Scanner scanner = new Scanner(str).useDelimiter(pattern);

    String token;
    while ((token = scanner.findInLine(pattern)) != null) {
        opts.add(token);
    }
    return opts;
}

So the incorrect output of the following code is

  • “Hello\”
  • World
  • ” “
  • Hello
  • Universe
  • Hi

EDIT I’m totally open to a non regex solution. It’s just the first solution that came to mind

  • 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-22T11:25:20+00:00Added an answer on May 22, 2026 at 11:25 am

    If you decide you want to forego regex, and do parsing instead, there are a couple of options. If you are willing to have just a double quote or a single quote (but not both) as your quote, then you can use StreamTokenizer to solve this easily:

    public static List<String> tokenize(String s) throws IOException {
        List<String> opts = new ArrayList<String>();
        StreamTokenizer st = new StreamTokenizer(new StringReader(s));
        st.quoteChar('\"');
        while (st.nextToken() != StreamTokenizer.TT_EOF) {
            opts.add(st.sval);
        }
    
        return opts;
    }
    

    If you must support both quotes, here is a naive implementation that should work (caveat that a string like ‘”blah \” blah”blah’ will yield something like ‘blah ” blahblah’. If that isn’t OK, you will need to make some changes):

       public static List<String> splitSSV(String in) throws IOException {
            ArrayList<String> out = new ArrayList<String>();
    
            StringReader r = new StringReader(in);
            StringBuilder b = new StringBuilder();
            int inQuote = -1;
            boolean escape = false;
            int c;
            // read each character
            while ((c = r.read()) != -1) {
                if (escape) {  // if the previous char is escape, add the current char
                    b.append((char)c);
                    escape = false;
                    continue;
                }
                switch (c) {
                case '\\':   // deal with escape char
                    escape = true;
                    break;
                case '\"':
                case '\'':  // deal with quote chars
                    if (c == '\"' || c == '\'') {
                        if (inQuote == -1) {  // not in a quote
                            inQuote = c;  // now we are
                        } else {
                            inQuote = -1;  // we were in a quote and now we aren't
                        }
                    }
                    break;
                case ' ':
                    if (inQuote == -1) {  // if we aren't in a quote, then add token to list
                        out.add(b.toString());
                        b.setLength(0);
                    } else {
                        b.append((char)c); // else append space to current token
                    }
                    break;
                default:
                    b.append((char)c);  // append all other chars to current token
                }
            }
            if (b.length() > 0) {
                out.add(b.toString()); // add final token to list
            }
            return out;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically I have 2 strings. They are EXACTLY the same string. However, when I
Basically I'm attempting to add rows to a table, I need to do this
Basically I need some text like: I have an ice cream cone. You are
Basically I just need the effect of copying that HTML from browser window and
Basically I'm trying to accomplish the same thing that mailto:bgates@microsoft.com does in Internet Explorer
Basically, what I need to do is update a record in my datatable without
Basically I need to create a client and server using only the local network,
Basically I need a nicely formatted stackpanel to appear over a control - permanently.
Basically, what I'm trying to create is a page of div tags, each has
Basically what I am doing is giving a user a list of terms via

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.