-2

Lets have a functions in C like this:

int main(){
    if(1){
      printf("1");
   }
}

What I need is to find the function body in { } and replace it with empty string. This bothers me because of the { }, which can be nested infinitely. Is there a way to find bodies like this or I will have to use the stack, or some variables to store the nesting.

gragas01
  • 25
  • 5
  • 2
    You cannot do it with just regex. Since regex is context insensitive. Like you said you can maintain a count of { and then you should be fine. – Ajay Brahmakshatriya Apr 07 '17 at 10:45
  • Possible duplicate of [RegEx match open tags except XHTML self-contained tags](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – ivan_pozdeev Apr 07 '17 at 10:52

1 Answers1

1

Unless this is a regex learning project undertaken solely for interest you should consider taking a more professional approach. You appear to have decided that regex is the appropriate approach without considering alternatives.

I'd suggest looking at using something like pycparser, which will do all the heavy lifting for you and present the program in a form that makes it easy to do what you want.

holdenweb
  • 33,305
  • 7
  • 57
  • 77