Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a text file with a lot of long file names. I am trying to match a line of text with different file extensions as follows (I know this code snippet doesn't work, but you get the idea).
$test = "some_text=\"asdfasdftest.123\""; $extensions="123,456"; if ($test =~ /some_text=".*\.$extensions"/i) { print "IT MATCHES\n"; }else{ print "It does not match\n"; }
In other words, I am looking for a match if the line equals:

some_text="something.123"
or
some_text="asdf.456"
but not
some_text="asdf.457"

I want to match that entire line with several different file extensions. I am hoping I can do it without a loop.

Replies are listed 'Best First'.
Re: File extension match
by revdiablo (Prior) on Aug 25, 2003 at 18:40 UTC

    Instead of:

    $extensions="123,456";

    You might want to try:

    $extensions = "(?:123|456)";

    Note that the ?: in the beginning of the parens makes this a non-capturing match.

    PS: For more information about regular expressions, see perlre

Re: File extension match
by tcf22 (Priest) on Aug 25, 2003 at 18:52 UTC
    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
      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

Re: File extension match
by bobn (Chaplain) on Aug 25, 2003 at 18:44 UTC

    First, use the qq() or q() function instead of escaping quotes:

    $test = qq[some_text="asdfasdftest.123"]; # or, because you weren't interpolating: $test = 'some_text="asdfasdftest.123"';
    Next, to deal with mulitple matches:
    # untested. @extensions = qw( 123 456 ); $match = join '|', @extensions; if ( $test =~ /($match)$/ ) # etc

    --Bob Niederman, http://bob-n.com

    All code given here is UNTESTED unless otherwise stated.