Regex scenarios to Capture Nested Parentheses in C# - Need guide with Grouping
This might be a silly question, but I'm sure I'm missing something obvious here, but I'm trying to extract mathematical expressions from a string that may contain nested parentheses, but my current regex isn't capturing the inner expressions correctly... For example, given the string `"(2 * (3 + 5)) - (4 / (2 - 1))"`, I want to match both `"(3 + 5)"` and `"(2 - 1)"` as separate groups but I'm only getting the outermost parentheses. I've tried using the following regex pattern: ```csharp string pattern = "\(([^()]*|(?R))\)"; ``` But I'm getting the behavior message `"Parsing behavior: unmatched parentheses"`. I believe the scenario might be related to the .NET regex engine not supporting recursive patterns, which I read in some documentation. I've also attempted to simplify my regex to just capture any parentheses with this pattern: ```csharp string simplePattern = "\(([^()]*)\)"; ``` However, this still only returns the outermost match, which isn't what I need. I need to capture all the nested expressions for further evaluation. Is there a different approach or tool I should be using in C# that could guide to with this kind of nested structure? Any suggestions or insight on how to work with nested parentheses in regex or alternatives would be greatly appreciated! My development environment is Linux. I'd really appreciate any guidance on this. The stack includes C# and several other technologies.