Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi I am trying to check if the #include's are of the form #include "word.h" or #include <word.h> (no spaces anywhere,should be exactly of this form) and also later I only want to get the header file name,for example in #include "string.h" or #include <string.h>,the header name is string.h.Can anyone advise how can I do that?

if ($line =~ /\#include "\w".h | \#include <\w>.h)) #check if +it is of the format #include "word.h" or #include <word.h> { my $header_file = $line =~/\#include \"(\w)\".h| /#include\<\ +w>.h> #get only the header file }

Replies are listed 'Best First'.
Re: Checking the include form and getting only the header name
by Eliya (Vicar) on Mar 19, 2011 at 23:11 UTC
    #!/usr/bin/perl -w use strict; while (my $line = <DATA>) { chomp $line; if ($line =~ /#include [<"](\w+\.h)[>"]/) { print "file is: $1\n"; } } __DATA__ #include "string.h" #include <string.h>

    Note that your usage of \w matches only one character — you'd need \w+ to match more than one.  Also, you have the .h outside of the <...> or "...".

      I am trying to get only the #include(.*).h part in a given line and using the below code which doesnt seem to be working for me.Can any one advise what is wrong?

      INPUT:- ./files/src/services/usb_dcd/private/internals.h:#include "queue.h" ./files/src/services/usb_dcd/private/queue.h:#include <sys/queue.h> $line =~ /\#include(.*)\.h/; # +get only the #include OUTUT:- #include "queue.h" #include <sys/queue.h>

        Try

        if ($line =~ /(#include.*\.h[>"])$/ ) { print "$1\n"; }

        or

        $line =~ s/.*(?=#include)//; print $line;