extract c/cpp include file with regular expression

MRAB python at mrabarnett.plus.com
Thu Jul 23 12:13:02 EDT 2009


tiefeng wu wrote:
> Hi all!
> I need to parse c/cpp source files, one requirement is to extract
> included header file name.
> here is my solution:
>>>> p = re.compile(r'#\s*include\s+(?:(<)|("))(.*)(?(1)>)(?(2)")')
>>>> m = re.search(p, '#include <header.h>')
>>>> m.group(3)
> 'header.h'
>>>> m = re.search(p, '#include "header.h"')
>>>> m.group(3)
> 'header.h'
>>>> m = re.search(p, '#include <header.h"')
>>>> print(m)
> None
>>>> m = re.search(p, '#include "header.h>')
>>>> print(m)
> None
> 
> Pretty ugly! And I know for a valid c/cpp source file, it will be not
> necessary to check and match '<' with '>' and " with ",
> but I'm wondering to see more elegant way to do such thing.
> 
I'd probably do:

 >>> p = re.compile(r'#\s*include\s+(?:<([^>]*)>|"([^"]*)")')
 >>> m = p.search('#include <header.h>')
 >>> m.group(1) or m.group(2)
'header.h'



More information about the Python-list mailing list