Split a string in java based on white spaces escaping those spaces in double quotes and single quotes and that which are preceded by \

java, regex, string

Solution

You can use this regex:

((["']).*?\2|(?:[^\\ ]+\\\s+)+[^\\ ]+|\S+)

RegEx Demo

In Java:

Pattern regex = Pattern.compile ( 
"(([\"']).*?\\2|(?:[^\\\\ ]+\\\\\\s+)+[^\\\\ ]+|\\S+)" );

Explanation:

This regex works on alternation:

- First match `([\"']).*?\\2` to match any quoted (double or single) strings.

- Then match `(?:[^\\ ]+\\\s+)+[^\\ ]+` to match any string with escaped spaces.

- Finally Use `\S+` to match any word with no spaces.

Problem

I am totally new to regular expressions. I'm trying to put together an expression that will split the example string using all spaces that are not surrounded by single or double quotes and are not preceded by a '\' Eg:- ``` He is a "man of his" words\ always ``` must be split as ``` He is a "man of his" words\ always ``` I understand ``` List<String> matchList = new ArrayList<String>(); Pattern regex = Pattern.compile("[^\\s\"']+|\"[^\"]*\"|'[^']*'"); Matcher regexMatcher = regex.matcher(StringToBeMatched); while (regexMatcher.find()) { matchList.add(regexMatcher.group()); } ``` l split the example string using all spaces that are not surrounded by single or double quotes How do I incorporate the third condition of ignoring the white-space if it is preceded by a \ ??

Original source