user1986402
user1986402

Reputation: 33

Split string by comma if previous character no \

I have a String, I need to split it with a comma, but if the previous character is \, then this part should not be splitted.

For example from String str = "first,second\\,third,fourth" i need String[] strs = { "first", "second\\,third", "fourth" }

Upvotes: 1

Views: 116

Answers (1)

Henry
Henry

Reputation: 43738

Something like this?

String str = "first,second\\,third,fourth";
String[] strs = s.split("(?<!\\\\),");

look at the java.utils.regex.Pattern documentation for an explanation.

Upvotes: 2

Related Questions