1

I have a text which is

SMA(20, V1) > 200000 AND C1 < LowerBollingerBand(20, 2)

after applying regex I need a result like this

SMA(20, V1) LowerBollingerBand(20, 2)

I can select SMA and LowerBollingerBand by defining these keywords explicitly like this

(SMA|LowerBollingerBand)(\()

But I am unable to select whatever comes between brackets followed by brackets

Haseeb Khan
  • 930
  • 3
  • 19
  • 41

1 Answers1

1

In case your strings cannot have nested balanced parentheses, you may use a [^()] negated character class to match any symbol but ( and ) after \( and add a \) after to match the close parentheses:

(SMA|LowerBollingerBand)\([^()]*\)

See the regex demo

Details:

  • (SMA|LowerBollingerBand) - either SMA or LowerBollingerBand
  • \( - a (
  • [^()]* - 0+ chars other than ( and )
  • \) - a ).

In case there are balanced nested parentheses, use

(SMA|LowerBollingerBand)\((?>[^()]+|(?<c>)\(|(?<-c>)\))*(?(c)(?!))\)

See this regex demo

The \((?>[^()]+|(?<c>)\(|(?<-c>)\))*(?(c)(?!))\) construct matches balanced nested parentheses, see this answer for a description. More on this can be found at the regular-expressions.info Balancing Groups.

Community
  • 1
  • 1
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563