if($a=~/ABC\s+XYZ/){ #do something }elsif($a=~/PQR\s+LMN/){ #do something }else{ print "Error\n"; #Want to know where exactly did the regex in above checks failed OR need to tell the user that your query failed at this particular place. }