Andre S. Veteran Posted June 2, 2009 Veteran Share Posted June 2, 2009 (edited) Hi, I need to parse C code and isolate the header names in the include directives. For instance if it's written #include "\..\..\header.h" or #include <stdio.h> I need to retrieve "header.h" or "stdio.h" . I also need its position in the string so that I can replace it with another header if necessary. I could do it with loops and conditions but is it feasible with a regular expression? Thanks. Edited June 2, 2009 by Dr_Asik Link to comment Share on other sites More sharing options...
0 code.kliu.org Posted June 2, 2009 Share Posted June 2, 2009 With PCRE, you can grab that with /#include\s+["<]([^">]+)[>"]/ or if you want to strip of any leading path components, with /#include\s+["<](?:[^">]*[\/\\])?([^\/\\">]+)[>"]/ As for the position of the match, that is dependent on your RE implementation; some will provide a position of the match, some will not... Link to comment Share on other sites More sharing options...
Question
Andre S. Veteran
Hi, I need to parse C code and isolate the header names in the include directives. For instance if it's written
#include "\..\..\header.h"
or
#include <stdio.h>
I need to retrieve "header.h" or "stdio.h" . I also need its position in the string so that I can replace it with another header if necessary.
I could do it with loops and conditions but is it feasible with a regular expression?
Thanks.
Edited by Dr_AsikLink to comment
Share on other sites
1 answer to this question
Recommended Posts