fork download
  1. import re
  2. source_content = '\n'.join([
  3. '#ifdef TYPEZERO',
  4. ' int someint = 42;',
  5. '#endif',
  6. 'void abc ( int value) {',
  7. ' return 5 ** 2.5',
  8. '}',
  9. '',
  10. 'abc',
  11. '#ifdef TYPEA', # <---- begin identifier, may contain leading/trailing whitespaces
  12. '#include "some_header.h"', # <---- I want these lines
  13. ' #include "some_other_header23.h"', # <---- I want these lines
  14. ' #else ', # optional stop identifier, may contain leading/trailing whitespaces
  15. 'double in_fact_int = 5;', # some irrelevant content
  16. ' #endif ', # final stop identifier, may contain leading/trailing whitespaces
  17. '',
  18. 'a = 5',
  19. '#ifdef TYPEB',
  20. ' abc = 23.5;',
  21. '#endif',
  22. ])
  23.  
  24. print(re.findall(r'#ifdef\s+TYPEA\s*(.*?)(?=\s*#(?:else|endif))', source_content, re.S))
Success #stdin #stdout 0.02s 9408KB
stdin
Standard input is empty
stdout
['#include "some_header.h"\n           #include "some_other_header23.h"']