Bavya Rajendran

Bavya Rajendran

  • NA
  • 14
  • 5.5k

How to get string between nested braces with using recursive

Jul 20 2018 8:59 AM
HI, I am trying to Split the String based on the brackets. String given below,
 
Input String: 
  1. String input = ADD((LENGTH(SUBSTRING(issue.quantity,FROM,1,2))),(LENGTH(issue.quantity1)))  
    I want a below output and stored into dynamic string array
 
Parent:
        ADD((LENGTH(SUBSTRING(issue.quantity,FROM,1,2))),(LENGTH(issue.quantity1)))
 
Child:
(LENGTH(SUBSTRING(issue.quantity,FROM,1,2))),(LENGTH(issue.quantity1))
 
Child of Child:
LENGTH(SUBSTRING(issue.receipt,FROM,1,7))
SUBSTRING(issue.quantity,FROM,1,2))
issue.quantity,FROM,1,2
 
Child of Child:
LENGTH(issue.quantity1)
issue.quantity1
 
|---ADD
         |----LENGTH
                     |----SUBSTRING
         |----LENGTH
 
   Because I want to rearrange this from end to start. I need to get the last child and after process of Child I have to merge it to Parent. likewise I have to merge each Child up to “ADD” again.
 
Here is my POC Code:
  1. public static void GetNestedCheckedItems(string input, int level, parentclass objparentclass)
    {
    var regex = new Regex(@"
    \( # Match (
    (
    [^()]+ # all chars except ()
    | (?<Level>\() # or if ( then Level += 1
    | (?<-Level>\)) # or if ) then Level -= 1
    )+ # Repeat (to go from inside to outside)
    (?(Level)(?!)) # zero-width negative lookahead assertion
    \) # Match )",

    RegexOptions.IgnorePatternWhitespace);
     
    foreach (Match c in regex.Matches(input))
    {
    Multiplestring1.Add(c.Value.Remove(c.Length - 1).Substring(1));
    }
     
    StringBuilder builder = new StringBuilder();
     
    foreach (var safePrime in Multiplestring1)
    {
    // Append each int to the StringBuilder overload.
    builder.Append(safePrime).AppendLine(Environment.NewLine);
    }
     
    result = builder.ToString();
     
    Multiplestring2.Add(result.Split(new string[] { "\r\n"},StringSplitOptions.RemoveEmptyEntries));

    Multiplestring1.RemoveAt(1);
    Multiplestring1.RemoveAt(0);
    GetNestedCheckedItems(result, level + 1, objparentclass);
    }
Here we couldn’t find any parent and child relationship.
 
Can anyone suggest me on this?
 
Thanks in Advance.