0

I'm looking for a regular expression to match , but ignore \, in Java's regex engine. This comes close:

[^\\],

However, it matches the previous character (in addition to the comma), which won't work.

Perhaps the regular expression approach is the wrong one altogether. I was intending to use String.split() to parse a simple CSV file (can't use an external library) with escaped commas.

David Chouinard
  • 5,676
  • 8
  • 41
  • 60

1 Answers1

7

You need a negative look-behind assertion here:

String[] arr = str.split("(?<![^\\\\]\\\\),");

Note that you need 4 backslashes there. First escape the backslash for Java string literal. And then again escape both the backslashes for regex.

Rohit Jain
  • 203,151
  • 43
  • 392
  • 509