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"
.