"scan header files to extract parts of #define and #include linesimport sysre pattdefine re compile'^#[\ ]*define[\ ]+(\ +)[\ ]**)'pattinclude re compile'^#[\ ]*include[\ ]+[<"]([\ /]+)'compile to pattobj "define xxx yyy \ like [ -za- - "include def scan(fileobj)count for line in fileobjscan by linesiterator count + matchobj pattdefine match(linenone if match fails if matchobjname matchobj group( substrings for parts body matchobj group( print(count'defined'name'='body strip()continue matchobj pattinclude match(lineif matchobjstartstop matchobj span( start/stop indexes of filename line[start:stopslice out of line print(count'include'filenamesame as matchobj group( if len(sys argv= scan(sys stdinelsescan(open(sys argv[ ]' ')no argsread stdin arginput filename to testlet' run this script on the text file in example - example - pp \lang\test #ifndef test_h #define test_h #include #include include "python #define debug #define hello 'hello regex worlddefine spam #define eggs sunny side up #define adder(arg arg #endif notice the spaces after in some of these linesregular expressions are flexible enough to account for such departures from the norm here is the script at workpicking out #include and #define lines and their parts for each matched lineit prints the line numberthe line typeand any matched substrings text and language