I have an input string containing a math expression that may contain comma-separated values that I need to remove they do not occur within an aggregation function. In those cases, I just want the first value to remain.
Consider the following example strings:
max ( 100,200,30,4 ) GOOD expression, do nothing
min ( 10,23,111 ) GOOD expression, do nothing
min ( 10,20 ) GOOD expression, do nothing
10,2,34 + 4 BAD expression, remove extra comma-number sequences => 10 + 4
So far I have tried surrounding a comma-number pattern (,\d+)+
with negative lookbehind/lookaheads:
str.replaceAll(/(?<!(max|min)\s\(\s\d+)(,\d+)+(?!\s\))/g, '');
However while this picks up the comma-number sequence outside of functions, this also incorrectly matches in valid situations as well:
max ( 100,200,30,4 ) GOOD expression
^^^ BAD match
min ( 10,23,111 ) GOOD expression
^^^ BAD match
min ( 10,20 ) GOOD expression
GOOD (non-match)
10,2,34 + 4 BAD expression
^^^^^ GOOD match
In each instance, I understand why it's matching but at a loss as to how to prevent it.
How can I do this?