in reply to simple regex match question

* and . have special meaning within regexes. If the string you're passing as a pattern into your regular expression contains an asterisk, that is going to be interpreted as a quantifier. And '.' is interpreted to mean "any character except newline".

You could probably do it like this:

if ( $file =~ m/\Q$file_pattern\E$/ ) { # .......

Also note the '$' which anchors the match to the righthand side of the filename. Otherwise, you might find yourself matching "filename.txt.xml" on accident.


Dave

Replies are listed 'Best First'.
Re^2: simple regex match question
by Anonymous Monk on Nov 13, 2006 at 06:07 UTC
    That doesn't seem to work...well, it works, but returns no files. Here is my test code:
    #!/usr/bin/perl use warnings; use Cwd; use File::Find; my $file_pattern ="*.txt"; find(\&d, cwd); sub d { my $file = $File::Find::name; return unless -f $file; return unless $file =~ /\Q$file_pattern\E$/; print "text file:$file\n"; }

      I didn't know what you were doing, but now I get it.

      The * character is not a wildcard in Perl. That's a shell wildcard. You cannot expect a regular expression to recognize shell-style wildcards; they're different "languages".


      Dave