extract c/cpp include file with regular expression
philip at semanchuk.com
Thu Jul 23 17:58:13 CEST 2009
On Jul 23, 2009, at 11:46 AM, 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 = re.search(p, '#include "header.h"')
>>>> m = re.search(p, '#include <header.h"')
>>>> m = re.search(p, '#include "header.h>')
> 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.
Regexes are always a little ugly IMO. =)
A side note -- does your parser need to handle /* comments like this
one*/? If so, then regular expressions are not going be sufficient.
More information about the Python-list