简体   繁体   English

Perl next if regex 存储在数组中

[英]Perl next if regex stored in array

i have a piece of code with aa while loop and a couple of next if's like this in the loop :我有一段带有 while 循环的代码和几个 next if 在循环中是这样的:

LOOP:
while (something) {
   next if $UPUPRF =~ /^T[0-9]{6}/;
   next if $UPUPRF =~ /^SECURITE.*/;
   next if $UPUPRF =~ /^AUDIT[A-Z]{2}/;
}

i would like to put these regexes in my configuation file (a perl data structure) instead of having them "hardcoded" in the script, so that the user can edit them easily by just editing the configuration file, and would like your advice regarding this.我想将这些正则表达式放在我的配置文件(perl 数据结构)中,而不是将它们“硬编码”在脚本中,以便用户只需编辑配置文件即可轻松编辑它们,并希望您对此提出建议.

what is the cleanest way of doing this ?这样做最干净的方法是什么?

first, i guess i can store the regexes like this in the configuration file ?首先,我想我可以在配置文件中存储这样的正则表达式吗? (with qr//): (使用qr//):

 ....
 exclude_acct => [
                    qr/^T[0-9]{6}/,
                    qr/^AUDIT[A-Z]{2}/,
                    qr/^SECURITE.*/,
                 ],
 ....

and in my loop, should i use something like this ?在我的循环中,我应该使用这样的东西吗? :

foreach (@{ $myhash{exclude_acct} }) {
    next LOOP if $UPUPRF =~ /$_/;
}

thanks for your advices.谢谢你的建议。

regards问候

note : i haven't tried this code yet, just guessing that i could do it like that, but i'm mainly wondering if this is "clean" or if there is a cleaner way of doing it注意:我还没有尝试过这段代码,只是猜测我可以这样做,但我主要想知道这是否“干净”或者是否有更干净的方法来做到这一点

That's fine, but starting the regex engine is relatively expensive, so it would be faster than create one regex from the many.这很好,但是启动正则表达式引擎的成本相对较高,因此比从众多正则表达式中创建一个正则表达式要快。

# Once, before the loop.
my $pat = join "|", @{ $myhash{exclude_acct} };
my $re = qr/$pat/;

# In the loop.
next LOOP if $UPUPRF =~ $re;

This can break backreferences ( \\1 ), though.不过,这可能会破坏反向引用( \\1 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM