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

6
  • @Wiktor Stribiżew Commented Oct 7, 2016 at 11:26
  • Can the parentheses be nested? Are you looking for just (SMA|LowerBollingerBand)\([^()]*\)? Commented Oct 7, 2016 at 11:26
  • not now but may be if any string becomes complex then there will nested parentheses Commented Oct 7, 2016 at 11:27
  • (SMA|LowerBollingerBand)([^()]*) its exactly what I wanted for now Commented Oct 7, 2016 at 11:29
  • My Big Thanks to you Commented Oct 7, 2016 at 11:29

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Done. Thanks again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.