-1

i have this paragraph :

"some texte  calculate((( a + b )/(c - d)) / 0.2) then some other text calculate((( a + b )/( c - d)) / 0.3)"

so i need a regex that can extract each calculate(.....) expression separately.

i appreciate if anyone could help me solve this issue. Thanks a lot

2 Answers2

0

This should be what your looking for:

UPDATE:

var str = "some texte  calculate(((a+b)/(c-d    ))/0.2) then some other text calculate(((a+b)/(c-d))/0.3)";

//calculate followed by recursive check for nesting
console.log(str.match(/calculate\((?:[^)(]+|\((?:[^)(]+|\([^)(]*\))*\))*\)+/g));
Prosy Arceno
  • 2,212
  • 1
  • 3
  • 18
  • hi thanks a lot, you answer works great when no space present, but i forget to mention that spaces could be present inside expression like so calculate((( a + b ) / ( c - d)) /0.2). any idea of how to do it ? – Nassim Mesdour May 04 '21 at 19:50
  • @NassimMesdour as long as the additional constraint is its within a parenthesis sure. i will update the code. – Prosy Arceno May 04 '21 at 20:10
  • @NassimMesdour, I only know basic regex but you can find out more on it on this [answer](https://stackoverflow.com/a/35271017/12415287). – Prosy Arceno May 04 '21 at 20:15
  • thank you man, i will check the link. i appreciate your help – Nassim Mesdour May 04 '21 at 21:09
  • @NassimMesdour I updated the answer. That should already be what you need – Prosy Arceno May 04 '21 at 21:13
  • Actually what if there is more nested parentheses ? i figure out that it is impossible to do it just with js regexp if we take in consideration the answer of @OnlineCop – Nassim Mesdour May 04 '21 at 21:17
0

Native javascript/ECMAScript doesn't support recursion, which is necessary for "any number of nested parens". If you can use XRegexp, you could use this recursive regex:

/calculate(?<braces>[(](?>[^()]+|(?&braces))*[)])/

You can see that here: regex101

OnlineCop
  • 3,799
  • 19
  • 33