在perl中提取特定字符串的完美正则表达式

Pus*_*ush 0 regex perl

我有一个abc.txt看起来像这样的文本文件:

dQdC(sA1B2C3,sC5) = A lot of stuff
a = b = c
Baseball
dQdC(sC2V3X1,sD5) = A lot of stuff again
Run Code Online (Sandbox Code Playgroud)

现在我想在Perl中创建两个数组,其中一个将包含A1B2C3C2V3X1,其他数组将包含C5D5.我不关心其他中间线.为了实现这个目标,我正在尝试这个perl脚本:

for (my $in=0;$in<=$#lines;$in++){
if ($lines[$in]=~/dQdC\(s([A-Z0-9]+?),s([A-Z0-9]+?)\)/) {
  print "1111"; #this line is just to check if it is at all going inside the loop
  @A = $1;
  @B = $2;
}
Run Code Online (Sandbox Code Playgroud)

但是,它甚至没有进入循环内部.所以我想我的正则表达式做错了.有人请告诉我这里做错了什么吗?

ike*_*ami 6

my (@a, @b);
while ($file =~ /^dQdC\(s(\w+),s(\w+)\)/mg) {
   push @a, $1;
   push @b, $2;
}
Run Code Online (Sandbox Code Playgroud)

要么

my (@a, @b);
while (<$fh>) {
   if (/^dQdC\(s(\w+),s(\w+)\)/) {
      push @a, $1;
      push @b, $2;
   }
}
Run Code Online (Sandbox Code Playgroud)

使用并行数组并不好.

备选方案1:哈希

my %hash = $file =~ /^dQdC\(s(\w+),s(\w+)\)/mg;
Run Code Online (Sandbox Code Playgroud)

要么

my %hash;
while (<$fh>) {
   if (/^dQdC\(s(\w+),s(\w+)\)/) {
      $hash{$1} = $2;
   }
}
Run Code Online (Sandbox Code Playgroud)

备选方案2:AoA

use List::Util qw( pairs );  # 1.29+

my @pairs = pairs( $file =~ /^dQdC\(s(\w+),s(\w+)\)/mg );
Run Code Online (Sandbox Code Playgroud)

要么

my @pairs;
while (<$fh>) {
   if (/^dQdC\(s(\w+),s(\w+)\)/) {
      push @pairs, [ $1, $2 ];
   }
}
Run Code Online (Sandbox Code Playgroud)