0

I have this string:

TABLEXY1((INITIALPARAM2*117.3/MADAD(2)),INITIALPARAM1+1)*PARAM2/100*(TABLE12(INITIALPARAM3)*PARAM3-(TABLE12(INITIALPARAM4)*PARAM4))"

i need to be able to get the TABLEXY1 with it values in the parentheses. the posibble values in there are:

(number)

(expression)

(number,number)

(number,expression)

(expression,expression)

(expression,number)

i don't know how many parentheses to expect.

I have tried

TABLEXY1\(((?<=\()(.*?)(?=\)))\)

but it gave me

TABLEXY1((INITIALPARAM2*117.3/MADAD(2)

and not what i needed

TABLEXY1((INITIALPARAM2*117.3/MADAD(2)),INITIALPARAM1+1)
Y.G.J
  • 1,098
  • 5
  • 19
  • 44

2 Answers2

2

If recursion is supported, then you could use:

TABLEXY1(\(([^()]|(?1))*\))

Live Demo

Since as far as I know C#'s regex doesn't support recursion. So instead you can just skip regex all together.

var str = "TABLEXY1((INITIALPARAM2*117.3/MADAD(2)),INITIALPARAM1+1)*PARAM2/100*(TABLE12(INITIALPARAM3)*PARAM3-(TABLE12(INITIALPARAM4)*PARAM4))";

int match = str.IndexOf("TABLEXY1");

if (match != -1)
{
    int begin = str.IndexOf("(", match) + 1;
    int end = 0;
    int parenthesisCount = 1;

    for (int i = begin; i < str.Length; ++i)
    {
        char c = str[i];

        if (c == '(')
            ++parenthesisCount;
        else if (c == ')')
            --parenthesisCount;

        if (parenthesisCount == 0)
        {
            end = i;
            break;
        }
    }

    Console.WriteLine(str.Substring(match, end - match + 1));
}

Which outputs:

TABLEXY1((INITIALPARAM2*117.3/MADAD(2)),INITIALPARAM1+1)
vallentin
  • 23,478
  • 6
  • 59
  • 81
  • This is a great technique for finding nested parenthesis. I was able to use this expression in AutoHotkey to exclude commas that were not in nested parenthesis. I posted a question and answer [here](https://stackoverflow.com/q/62806431/1898524). – Ben Jul 09 '20 at 02:33
0

Did you meant to something like this?

"(?=TABLEXY1.+()\S+(?=*PARAM2.*)"

the answer I got is: TABLEXY1((INITIALPARAM2*117.3/MADAD(2)),INITIALPARAM1+1)

Dana S
  • 53
  • 1
  • 12
  • No, You can't be specific, i don't know what in the first (main) parenthesys. where did you check it? i don't get the same – Y.G.J Apr 16 '17 at 10:37
  • powershell... I didnt specify anything .. I said after you saw ( take everything until you see the word PARAM2.. – Dana S Apr 16 '17 at 10:44