my $pattern='/^LOC_Os0[1-7]g[0-9]*.[0-9]\s'; ... $line1=~ /^$pattern/; # $line1 (and 2) will expand to smth like /^^LOC_0s.../, which is probably not what you want. Try my $pat = qr/^LOC_0s.../ $line1 =~ $pat;