• 0

Regular expression help


Question

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_Asik
Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

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

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.