Skip to main content
added 79 characters in body
Source Link
Keith C
  • 137.3k
  • 29
  • 218
  • 458

This works:

string s1 = '"five1,five2", six ,seven,"eig\\"ht1,eight2","nine",,eleven';

String lst = s1.replaceAll('\\\\"','\\$');
System.debug(lst);

See e.g. Can't escape the backslash with regex? for some detail on needing multiple levels of escaping.

But the quick explanation is that for a backslash to be treated literally by the Apex compiler it needs to be escaped itself by a backslash, and then another layer of escaping is needed for regex engine too. Hence 2 * 2 = 4 backslashes needed.

And $ has special meaning in regex too, so that character needs escaping.

This works:

string s1 = '"five1,five2", six ,seven,"eig\\"ht1,eight2","nine",,eleven';

String lst = s1.replaceAll('\\\\"','\\$');
System.debug(lst);

See e.g. Can't escape the backslash with regex? for some detail on needing multiple levels of escaping.

But the quick explanation is that for a backslash to be treated literally by the Apex compiler it needs to be escaped itself by a backslash, and then another layer of escaping is needed for regex engine too. Hence 2 * 2 = 4 backslashes needed.

This works:

string s1 = '"five1,five2", six ,seven,"eig\\"ht1,eight2","nine",,eleven';

String lst = s1.replaceAll('\\\\"','\\$');
System.debug(lst);

See e.g. Can't escape the backslash with regex? for some detail on needing multiple levels of escaping.

But the quick explanation is that for a backslash to be treated literally by the Apex compiler it needs to be escaped itself by a backslash, and then another layer of escaping is needed for regex engine too. Hence 2 * 2 = 4 backslashes needed.

And $ has special meaning in regex too, so that character needs escaping.

Source Link
Keith C
  • 137.3k
  • 29
  • 218
  • 458

This works:

string s1 = '"five1,five2", six ,seven,"eig\\"ht1,eight2","nine",,eleven';

String lst = s1.replaceAll('\\\\"','\\$');
System.debug(lst);

See e.g. Can't escape the backslash with regex? for some detail on needing multiple levels of escaping.

But the quick explanation is that for a backslash to be treated literally by the Apex compiler it needs to be escaped itself by a backslash, and then another layer of escaping is needed for regex engine too. Hence 2 * 2 = 4 backslashes needed.