0

I am trying to parse a string and want to replace All '(' with ' ( ' when I try to execute the following command

String s1 = '1AND( 2  OR  3  )  AND4 AND(5OR6)';
s1=s1.replaceAll( '(' , ' ( ' );

I get the following error

FATAL_ERROR System.StringException: Invalid regex: Unclosed group near index 1

When I try to use replace instead of replaceAll it works But I want to use replace all, please suggest me how to get it to work.

1 Answer 1

4

You simply need to update your replacement string to use an escape against "(" since these are special characters in regex.

Try the following:

String s1 = '1AND( 2  OR  3  )  AND4 AND(5OR6)';
s1=s1.replaceAll( '\\(' , ' ( ' );

It is worth reading the Salesforce documentation for String to understand the difference between replace and replaceAll. For literal replacements such as this one you probably can use String.replace.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .