in reply to Checking the include form and getting only the header name

#!/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 "...".

Replies are listed 'Best First'.
Re^2: Checking the include form and getting only the header name
by Anonymous Monk on Mar 20, 2011 at 01:15 UTC

    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;

        Sometimes there are comments at the end,so that scenario also needs to be takin into account

        /files/src/services/usb_dcd/private/internals.h:#include "queue.h" /*c +omments*/