#!/usr/bin/perl -w use strict; while (my $line = ) { if ( my $include_file = ($line =~ /#include\s+[<"](.*?)[>"]/)[0] ) { print "$include_file\n"; } } #prints: #string.h #stdlib.h #sys/dispatch.h __DATA__ INPUT:- #include "string.h" --> below code prints header this is just a BS, nonsense line #include --> below code prints header #include --> not able to get the header name(dispatch.h)