The value of $ur is "rba_Reset_Request". foreach my $ur (@strings_to_be_matched) { $reg1 = qr/\=/i; #line has a single '=' #/i makes no difference, delete that! #there is no "capital ="! $reg2 = qr/\S+\=\S+/i; #line has a single "=" with something #to the left and something to the right $reg3 = qr/extern.+\b$ur\b\s*/i; #line has extern rba_Reset_Request $reg4 = qr/;$/i; #line ends in ";" # in C code you are not allowing for the #idea that whitespace may be after the ";" # /;\s*$/; $reg5 = qr/.+\b$ur\b\s*/i; #line has rba_Reset_Request #could just be: #/\b$ur\b/; ? foreach my $line (@contents_of_file) { if(($ln =~ $reg3 and $ln =~ $reg4)){ # line contains: extern rba_Reset_Request # and ends in a ; } if(($ln =~ $reg5 and $ln=~ $reg4 and ($ln !~ $reg1 or $ln =~ $reg2)) { # line contains rba_Reset_Request, ends in a ";", has an "=" sign. } if(($ln =~ $reg3 and $ln !~ $reg4)){ # line contains extern rba_Reset_Request and doesn't end in ; } if(($ln =~ $reg5 and $ln !~ $reg4 and $ln !~ $reg1)) #line contains rba_Reset_Request doesn't end in ; or # have an = sign { } } }