in reply to File extension match

If you wanted to check a list of thing, which I think would probably be the case, you use something like this
use strict; use warnings; my @extensions = qw( 123 456 ); my $extlist = join('|', @extensions); my $re = qr/some_text=".*\.(?:$extlist)"/; while(<DATA>){ chomp; if ($_ =~/$re/i) { print "$_ MATCHES\n"; }else{ print "$_ does not match\n"; } } __DATA__ some_text="something.123" some_text="asdf.456" some_text="asdf.457"

OUTPUT:
some_text="something.123" MATCHES some_text="asdf.456" MATCHES some_text="asdf.457" does not match

Replies are listed 'Best First'.
Re^2: File extension match
by flounder99 (Friar) on Aug 25, 2003 at 19:38 UTC
    I would like to add a few things I've learned from experience.
    I would change:
    my $extlist = join('|', @extensions);
    to
    my $extlist = join('|', map {quotemeta} @extensions);
    in case members of @extensions contains some metacharacters. There might be an extension file.(myextension)
    I'd also change:
    my $re = qr/some_text=".*\.(?:$extlist)"/;
    to
    my $re = qr/some_text=".*\.(?:$extlist)"$/;
    because you never know if someone has a file named something like "file.123".backup
    It doesn't matter in these simple examples but when you try using it in the real world you never know what you will get. I try to think of pathological cases because most users are, well, pathological. Someone else will probably have some better suggestions on my code. They will probably be speaking from experience (of pathological users).

    --

    flounder