Like the question says, is it possible to use a single Regex string to get a delimiter that isn't in between some quotes?
For example, I want to split this string with the delimiter &
:
"example=3&testing='f&tmp'"
should produce
["example=3", "testing='f&tmp'"]
Essentially, things inside single quotes (' '
) should remain untouched.
I found out how to get things within quotes with expression: (?:'.*?')
The closest I could get to a tangible solution was: (.[^']&[^'])
CodePudding user response:
It is not an easy task for a String#split
, but is quite a feasible task for Matcher#find
if you use
[^&\s=] =(?:'[^']*'|[^\s&]*)
(see this regex demo) and this Java code:
String text = "example=3&testing='f&tmp'";
Pattern p = Pattern.compile("[^&\\s=] =(?:'[^']*'|[^\\s&]*)");
Matcher m = p.matcher(text);
List<String> res = new ArrayList<>();
while(m.find()) {
res.add(m.group());
}
System.out.println(res);
// => [example=3, testing='f&tmp']
Details
[^&\s=]
- one or more chars other than&
,=
and whitespace=
- a=
char(?:'[^']*'|[^\s&]*)
- a non-capturing group matching either'
, zero or more chars other than'
and then a'
, or zero or more chars other than whitespace and&
.