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 matchList = new ArrayList();
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 \ ??
解决方案
You can use this regex:
((["']).*?\2|(?:[^\\ ]+\\\s+)+[^\\ ]+|\S+)
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.