Find and replace equations (regular expression?)

I am processing some excel formulas into another system and should do a rather sophisticated search and replace magic. I assume that Regex are the tools to work in this case, but if anyone has any other ideas, I would like to hear them.

I am working to ensure that these formulas are somewhat similar to SQL syntax. I also have to deal with algebraic characters, so I might have the following:

9 ^ 2 should become POWER (9.2). (A + 3) ^ 3 should become POWER ((A + 3), 3).

What is the best approach for this?

I use C # 3.5 if that matters.

edit: An example of what I need to analyze (the power symbol is near the end):

"((({VAL(9286)} / 1000) * {VAL(4648)}) + (({VAL(9609)} / 1000) + ({VAL(6480)} / 1000)) * {VAL(8574)}) / ({VAL(9286)} / 1000 + {VAL(9609)} / 1000 + {VAL(6480)} / 1000) * (({VAL(9286)} / 22.4)*34.38 + {VAL(9609)} + {VAL(6480)}) * ((1.075068 + 0.001*11.17019 * ((({VAL(9286)} / 1000) * {VAL(4648)}) + (({VAL(9609)} / 1000) + ({VAL(6480)} / 1000)) * {VAL(8574)}) / ({VAL(9286)} / 1000 + {VAL(9609)} / 1000 + {VAL(6480)} / 1000)+273.15)) + (100000*0.90755 / ((({VAL(9286)} / 1000) * {VAL(4648)}) + (({VAL(9609)} / 1000) + ({VAL(6480)} / 1000)) * {VAL(8574)}) / ({VAL(9286)} / 1000 + {VAL(9609)} / 1000 + {VAL(6480)} / 1000) + 273.15)^2))*4.1868/32)"
+3
source share
1

- . .

. ,

((?=[\w.(])[\w.]*\s*(?:\((?>[^()]+|\((?<O1>)|\)(?<-O1>))*(?(O1)(?!))\))?)\s*\^\s*((?=[\w.(])[\w.]*\s*(?:\((?>[^()]+|\((?<O2>)|\)(?<-O2>))*(?(O2)(?!))\))?)

"POWER($1,$2)"

, a^b POWER(a,b). :

   (a+3)^(b+5^(c+3)) + 9 ^ 2 + (A + 3)^3 + (5^7)^(6^(8^9-1)-3)
-> POWER((a+3),(b+5^(c+3))) + POWER(9 ,2 )+ POWER((A + 3),3 )+ POWER((5^7),(6^(8^9-1)-3))
-> POWER((a+3),(b+POWER(5,(c+3)))) + POWER(9 ,2 )+ POWER((A + 3),3 )+ POWER((POWER(5,7)),(POWER(6,(8^9-1))-3))
-> POWER((a+3),(b+POWER(5,(c+3)))) + POWER(9 ,2 )+ POWER((A + 3),3 )+ POWER((POWER(5,7)),(POWER(6,(POWER(8,9)-1))-3))
-> done

, , ^ -, -.

   1^2^3
-> POWER(1,2)^3
-> POWER(POWER(1,2),3)
-> done

1 ^ 2 ^ 3 .

+2

Source: https://habr.com/ru/post/1757631/


All Articles