java split string on two or more spaces except for words in quotes

Solutions on MaxInterview for java split string on two or more spaces except for words in quotes by the best coders in the world

showing results for - "java split string on two or more spaces except for words in quotes"
Nicky
30 Jun 2019
1String str = "Location \"Welcome  to india\" Bangalore " +
2             "Channai \"IT city\"  Mysore";
3
4List<String> list = new ArrayList<String>();
5Matcher m = Pattern.compile("([^\"]\\S*|\".+?\")\\s*").matcher(str);
6while (m.find())
7    list.add(m.group(1)); // Add .replace("\"", "") to remove surrounding quotes.
8
9
10System.out.println(list);
Leo
04 Jan 2020
1[Location, "Welcome  to india", Bangalore, Channai, "IT city", Mysore]