How do I build Perl regular expressions dynamically?

后端 未结 6 1638
-上瘾入骨i
-上瘾入骨i 2021-02-08 15:24

I have a Perl script that traverses a directory hierarchy using File::Next::files. It will only return to the script files that end in \".avi\", \".flv\", \".mp3\", \".mp4\", a

6条回答
  •  眼角桃花
    2021-02-08 16:00

    If you want to build a potentially large regexp and don't want to bother debugging the parentheses, use a Perl module to build it for you!

    use strict;
    use Regexp::Assemble;
    
    my $re = Regexp::Assemble->new->add(qw(avi flv mp3 mp4 wmv));
    
    ...
    
    if ($file =~ /$re/) {
        # a match!
    }
    
    print "$re\n"; # (?:(?:fl|wm)v|mp[34]|avi)
    

提交回复
热议问题