Regular or other methods to remove specified characters or strings

topic description

A string in which multiple repeated characters are removed, but do not know the relevant regular method

related codes

    String ss="SUM(TA0047/(TA0012+TA0023+SUM(TA0034+TA0045)))";

what result do you expect? What is the error message actually seen?

the result is TA0047/ (TA0012+TA0023+TA0034+TA0045)

problem description

removing api replace or replaceAll from SUM () java can only remove SUM, but the position of parentheses is not fixed. I don"t know what to do so far. After parsing into char, the location of the tag is queried in a loop, but the train of thought is not clear. Ask for help

May.22,2021

just give up the regularity. it's almost impossible.
I am not familiar with java, using javascript to implement a version, using regular plus recursion, the principle:

  1. find the innermost pair of parentheses each time and determine whether they are SUM

    .
    1. if it is SUM, remove SUM ( and )
    2. if it is not SUM, replace this pair of parentheses with a custom alternative symbol temporary in order to continue to expand the outer parenthesis pairs for the next round of recursion.
  2. After all the
  3. is processed, replace the placeholders of the left and right parentheses with the left and right parentheses to get the desired string.

js java javascript node.js javascript

<ol> <li>

/<strong>SUM()</strong> javascript

let r = SUM(a+b)/c;
const CHECKSUM = 100;
let log = "SUM(1+2)?";
  • if you only need to consider arithmetic operations (addition, subtraction, multiplication and division), that is, when the result of SUM (a) equals (a) , you can directly delete SUM and retain the parentheses.
  • if you consider the complete javascript environment, you can insert a SUM definition function, such as

    function SUM(n) {
      return n;
    }

    so that the original string does not need to be modified. Java example

    String injectScript = "function SUM(n) {return n;};";
    String ss="SUM(TA0047/(TA0012+TA0023+SUM(TA0034+TA0045)))";
    jsExec(injectScript + ss);
  • Menu