2

this is my patten: (TABLE[0-9]+)\(((?<=\()(.*?)(?=\)))[(?=\))\)]

this is the string i'm looking at: (TABLE3(1.6+TABLE1(2)))*TABLE2(1)*TABLE11(1)*(1.19017735023328)

what i get are:

  • TABLE3(1.6+TABLE1(2)
  • TABLE2(1)
  • TABLE11(1)

i need the first one to be: TABLE3(1.6+TABLE1(2))

how can i do that?

Willy David Jr
  • 8,604
  • 6
  • 46
  • 57
Y.G.J
  • 1,098
  • 5
  • 19
  • 44

1 Answers1

3

Use a balancing group construct after TABLE[0-9]+:

TABLE[0-9]+\((?>[^()]|(?<o>)\(|(?<-o>)\))*(?(o)(?!))\)

See the regex demo.

enter image description here

Details

  • TABLE[0-9]+ - matches TABLE and 1+ digits
  • \( - an open (
  • (?>[^()]|(?<o>)\(|(?<-o>)\))* - zero or more occurrences of
    • [^()] - any char but ( and )
    • | - or
    • (?<o>)\( - a ( (and increments the stack of the o group)
    • | - or
    • (?<-o>)\) - a ) (and decrements the stack of the o group)
  • (?(o)(?!)) - fail the match (triggers backtracking) if Group o stack is not empty
  • \) - a ).
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Thank you for the very detailed explanation of how the balancing works. The confusing part for me is (?)\\( or (?<-o>)\\). It seems that brackets for the counting groups are not closed. I have changed the regex to balance the brackets in the counting groups, and it still works the same. TABLE[0-9]+\\(([^()]|(?\\()|(?<-o>\\)))*(?(o)(?!))\\). What am I missing here? – LetzerWille Aug 28 '17 at 15:48
  • @LetzerWille: You are missing the fact that you do not need to store the `(` or `)` inside the CaptureCollection of Group "o". You may as well store empty values in the stack. My solution just adds up and then removes empty strings, while you add up and remove `(` and `)` chars. Well, it means that *It seems that brackets for the counting groups are not closed* is not a correct statement. – Wiktor Stribiżew Aug 28 '17 at 15:53