Split string on spaces in Java, except if between quotes (i.e. treat \"hello world\" as one token)

Java

Java Problem Overview


How do I split a String based on space but take quoted substrings as one word?

Example:

Location "Welcome  to india" Bangalore Channai "IT city"  Mysore

it should be stored in ArrayList as

Location
Welcome to india
Bangalore
Channai
IT city
Mysore

Java Solutions


Solution 1 - Java

Here's how:

String str = "Location \"Welcome  to india\" Bangalore " +
             "Channai \"IT city\"  Mysore";

List<String> list = new ArrayList<String>();
Matcher m = Pattern.compile("([^\"]\\S*|\".+?\")\\s*").matcher(str);
while (m.find())
    list.add(m.group(1)); // Add .replace("\"", "") to remove surrounding quotes.


System.out.println(list);

Output:

[Location, "Welcome  to india", Bangalore, Channai, "IT city", Mysore]

The regular expression simply says

  • [^"]     - token starting with something other than "
  • \S*       - followed by zero or more non-space characters
  • ...or...
  • ".+?"   - a "-symbol followed by whatever, until another ".

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1000535View Question on Stackoverflow
Solution 1 - JavaaioobeView Answer on Stackoverflow