• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2057
  • Last Modified:

Regex Parse Brackets { ... }

I am new regarding Regex strings.
Could someone please provide me a sample Regex that matches a while statement that includes the contents of the while statement's open and close curly braces?  I need a Regex string that will work in C#.

See the 2 code snippets below.  The first section of code is an example original string, while the second section of code is the matched string.  Notice the while loop contains a nested if/else statement.  I need a regex string that will work with any number of nested code inside the while loop

=========================================
// Original String:

int a = 0;
int b = 2;

if(false) {
        // ...
}


while(SomeThingIsTrue){
      if(true) {
            // ...
      } else {
            // ...
      }
}

return a + b;

=========================================
// Matched String:

while(SomeThingIsTrue){
      if(true) {
            // ...
      } else {
            // ...
      }
}

=========================================


THANK YOU !
0
mdtanner
Asked:
mdtanner
  • 3
  • 2
  • 2
1 Solution
 
Terry WoodsIT GuruCommented:
It's quite tricky, unless perhaps you assume that the closing curly brace } has the same indentation as the word "while" - is that a fair assumption?
0
 
Terry WoodsIT GuruCommented:
The reason it's tricky is that nested brackets need pairing up, and text that's in quotes as part of the contained code may contain brackets that shouldn't be considered part of the nesting - this means quotes need to be paired up too (the standard term seems to be "balanced", I think).
0
 
mdtannerAuthor Commented:
Thanks Terry,

For right now I'm not too concerned with possible curly braces in quoted strings or in comments.  I am mainly interested in learning how to match open and closed blocks (such as curly braces).  Once I've learned how this works I can tackle the more complicated stuff later.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Terry WoodsIT GuruCommented:
This might help:
http://workwith.net/faq/csharp/#_Toc172381886

I'm not a C# programmer, so I'm not 100% sure this will work, but here's the pattern from the above link modified to match curly braces - I think backslashes need doubling up for C#?
string pattern = "^[^\\{\\}]*" +
                 "(" +
                 "((?'Open'\\{)[^\\{\\}]*)+" +
                 "((?'Close-Open'\\})[^\\{\\}]*)+" +
                 ")*" +
                 "(?(Open)(?!))$";

Open in new window

0
 
SuperdaveCommented:
It's impossible in theory.

You can make increasingly complicated expressions to match to a maximum depth of nesting, for example this will match up to one level of nesting:

"{[^{}]*({[^{}]*}[^{}]*)*}"

The part in the middle in parentheses matches the nested {} plus anything after it up to the next { or } so that it can be repeated to match any number of inner {} as long as they only nest one level.

If you made it a bit more complicated you could have another parenthesized expression in the middle to match 0 or more second-level nestings, and so on, but you can't match an arbitrary number,

That is, unless C# is substantially different then Python regular expressions, but I think they are the same.
0
 
SuperdaveCommented:
Oh, sorry, I read the above answer and the link:  apparently C# "regular expressions" are more powerful than the Python equivalent.  It's about time for them to come up with a different name for them!
0
 
mdtannerAuthor Commented:
The answer I was given seemed to be correct.  But the answer to my question is that there isn't an answer.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now