Need to split a string using delimiter, but only if there is no backslash before the delimiter.
ex: if there is abc \:abc
- do not split it as :
has backslash before it.
if the string is abc : abc
- need to split as abc, abc
.
The delimiters can be :,|,&
etc.
Use a negative look-behind (?<!...)
. To match a literal backslash \
, you have to escape twice. Once to escape because it's a string literal, and again because it's a regex escape character.
String[] parts = string.split("(?<!\\\\)[:,|&]");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With