Your code will also match a file named pretxt.
my $exten = "(\.txt|\.csv)";
should be
my $exten = "(\\\.txt|\\\.csv)"; # (\.txt|\.csv)
There's no reason to force the user to specify the parens.
Why did you remove the case-insensitivity?
You also forgot to anchor the regexp.
use strict; use warnings; my $exten = "\\\.txt|\\\.csv"; # \.txt|\.csv my @files = qw( one.txt two.html three.csv four.log pretxt file.txt~ File.Txt ); foreach (@files) { if (/($exten)\z/i) { print $_, "\n"; } }
Update: Or if you want to build $exten:
my @exten = qw( .txt .csv ); my $exten = join('|', map quotemeta, @exten);
In reply to Re^2: how to get the String as regularexpression
by ikegami
in thread how to get the String as regularexpression
by arunmep
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |