replaceAll trows an error when the string contains brackets (as text)
Hi community, replaceAll throws an error when the string contains brackets (as text), any clue on how to solve this? This is a simple reconstruction of the problem: $SquadWIP = 'Client Tribe CH, Add on business billing (V-Zug, Insel and small customers)' $VariableSplittedAndTrimmed = ', Insel and small customers)' the Change variable = replaceAll($SquadWIP, $VariableSplittedAndTrimmed, '') Stack trace: com.mendix.webui.WebUIException: Exception while executing runtime operation at com.mendix.webui.actions.client.RuntimeOperationAction.$anonfun$apply$1(RuntimeOperationAction.scala:54) Caused by: com.mendix.modules.microflowengine.MicroflowException: Failed to evaluate expression, error occurred on line 1, character 1 replaceAll($SquadWIP, $VariableSplittedAndTrimmed, '') ^ at Masterdata.ACT_replaceAll_Test (CreateOrChangeVariable : 'Change variable Result') Advanced stacktrace: at com.mendix.languages.mxexpressions.MxExpressionImpl.evaluate(MxExpressionImpl.scala:32) Caused by: com.mendix.languages.expressions.ExpressionException: Unmatched closing ')' near index 26 , Insel and small customers) ^ at com.mendix.languages.expressions.Expr.evaluate(Expr.scala:16) Any hint? I tried CommunityCommons RegexReplaceAll but it has similar problems.
Hi Marco, only add one \ in front of the ) in the string VariableSplittedAndTrimmed:
That’s all. Then run the microflow again, and to prove that it works, see this screenshot having a breakpoint just after the ReplaceAll-function was completed, and with the values of the variables visible:
This also works if you would want to replace both braces and everything in it: (V-Zug, Insel and small customers), just set VariableSplittedAndTrimmed to this value:
Tim van Steenbergen
It seems like the error is caused due to the parenthesis characters. This is because the replaceAll function has the second parameter of a Regular Expression (see documentation here).
To get around this issue you need to escape such characters with “\”.
For example this:
Client Tribe CH, Add on business billing (V-Zug, Insel and small customers)
Should be converted into this:
Client Tribe CH, Add on business billing \(V-Zug, Insel and small customers\)
This is now a valid regular expression:
A simple alternative (if you don't really want to use regular expressions) is to try using “\Q” and “\E” too for literal strings, example:
“\QClient Tribe CH, Add on business billing (V-Zug, Insel and small customers)\E”