Splitting Strings on Spaces, Preserving Quoted Substrings in Java
Splitting a string on spaces can be straightforward, but it becomes more complex when quoted substrings need to be treated as a single token. Let's explore how to achieve this in Java.
Problem Statement:
How can we split the following string on spaces, considering that quoted substrings remain as one unit?
Location "Welcome to india" Bangalore Channai "IT city" Mysore
The desired output should be stored in an array list, preserving the quoted substrings:
[Location, "Welcome to india", Bangalore, Channai, "IT city", Mysore]
Solution:
By utilizing regular expressions, we can define a pattern that matches substrings that are either non-space characters ("1S") or quoted substrings (""(. ?)""). The pattern further allows for optional whitespace characters ("s") following the matches.
String str = "Location \"Welcome to india\" Bangalore " + "Channai \"IT city\" Mysore"; List<String> list = new ArrayList<>(); 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);
In this solution:
Output:
[Location, "Welcome to india", Bangalore, Channai, "IT city", Mysore]
The above is the detailed content of How to Split a String on Spaces While Preserving Quoted Substrings in Java?. For more information, please follow other related articles on the PHP Chinese website!