0

what regex could remove whole function body even, if curly brackets are nested ? What I have so far is re.sub('{.*?}', '', source) however, it doesn't work with nested brackets so it wont remove line 5 and 6. I know that similiar question to this is already written here several times, however, still cannot find solution

1. int foo(void){
2.    if(cond){
3.        code
4.    }
5.    next few
6.    lines of code
7. }
10. typedef struct strfoo{
11.    code
12. }

thanks in advance

jpact
  • 1,042
  • 10
  • 23
  • In this case you'd want to use a *stack* to know when you got back to the outer-most level of braces. To do this you'd want to push open braces onto the stack, and pop them off when you see a closing brace. Problems that are stack-based in nature are not good candidates for regex. – turbulencetoo Apr 11 '14 at 14:56
  • See http://stackoverflow.com/questions/133601/can-regular-expressions-be-used-to-match-nested-patterns. – robx Apr 11 '14 at 14:56
  • Thanks for replies guys, it looks like that i will have to do that via finite state automat, wanted to avoid that, but... Thanks anyway :) – jpact Apr 11 '14 at 14:59

0 Answers0