perl regexp matching

Hello,

I cannot see what's wrong in my code.
When I run code below, it just print an empty string.

my $test = "SWER~~ERTGSDFGTHAS_RTAWGA_DFAS.x4-234253454.in";
if ($test = ~ m/\~{1,2}[A-Z].*[xX]4/) {
   print "$1\n";
}
else {
   print "No match...\n";
}

Anyone know what I'm doing wrong?

Thanks in advance!

Because you ain't got a matching group () anywhere that could put something into $1.

Okay, but should it not at least run the else statment then?

I thought my regular expression would match "~~ERTGSDFGTHAS_RTAWGA_DFAS.x4" ?

Perhaps anyone can show me how I match above string?

Yes, it does match. Which is why it's not moving into the else branch. But without a matching group it doesn't know what to put into $1, so it just prints an empty line.

Compare your code with this:

my $test = "SWER~~ERTGSDFGTHAS_RTAWGA_DFAS.x4-234253454.in";
if ($test = ~ m/(~{1,2}[A-Z].*[xX]4)/) {
   print "$1\n";
}
else {
   print "No match...\n";
}

Ah okay, now I understand :slight_smile:

Thanks a lot.