regex - 在数组中携带多个捕获组但只匹配一个组

标签 regex perl

我正在尝试对一些现有代码进行简单修改,但运气不佳,我想从 FILE1 中再携带一个捕获组:$2,然后像往常一样将 $1 与 FILE2 中的数据进行比较,如果匹配成功则打印两者.如果可能,请保持与我的尝试相似的答案,以便我能够理解更改。

FILE1 数据:

abc 99269 +t
abc 550 -a
abc 100 +a
gdh 126477 +t 
hduf 1700 +c

FILE2 数据:

517 1878 forward
2156 3289 forward
99000 100000 forward
22000 23000 backward
999555 999999 backward 

期望的输出:

99269 +t 99000 100000 forward
550 -a 517 1878 forward
1700 +c 517 1878 forward 

代码:

#!/usr/bin/perl 

use strict;
use warnings;
use autodie;

my $outputfile = "/Users/edwardtickle/Documents/CC22CDSpositive.txt"; 

open FILE1, "/Users/edwardtickle/Documents/CC22indelscc.txt";

open FILE2, "/Users/edwardtickle/Documents/CDS_rmmge.CC22.CORE.aln";

open (OUTPUTFILE, ">$outputfile");
my @file1list=();
my @indels=();

while (<FILE1>) {
    if (/^\S+\s+(\d+)\s+(\S+)/) {
        push @file1list, $1;
        push @indels, $2;
    }
}

close FILE1;

while ( my $line = <FILE2> ) {
    if ($line =~ /^>\S+\s+\S+\s+(\d+)\s+(\d+)\s+(\S+)/) {
        my $cds1 = $1;
        my $cds2 = $2;
        my $cds3 = $3;

        for my $cc22 (@file1list) {
            for my $indel (@indels) {
                if ( $cc22 > $cds1 && $cc22 < $cds2 ) {
                    print OUTPUTFILE "$cc22 $indel $cds1 $cds2 $cds3\n";
                }
            }
        }
    }
}

close FILE2;
close OUTPUTFILE;

提前致谢!

最佳答案

令人沮丧的是,您似乎没有从已获得的许多解决方案和建议中学习。

这是一个可以按您的要求执行的程序。

use strict;
use warnings;
use 5.010;
use autodie;

chdir '/Users/edwardtickle/Documents';

open my $fh, '<', 'CDS_rmmge.CC22.CORE.aln';

my @file2;
while (<$fh>) {
  next unless /\S/;
  push @file2, [ split ];
}

open my $out, '>', 'CC22CDSpositive.txt';

open $fh, '<', 'CC22indelscc.txt';

while (<$fh>) {

  my @line1 = split;

  for my $line2 (@file2) {

    if ( $line1[1] >= $line2->[0] and $line1[1] <= $line2->[1] ) {
      my @out = ( @line1[1,2], @$line2 );
      print $out "@out\n";
      last;
    }
  }
}

关于regex - 在数组中携带多个捕获组但只匹配一个组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26831338/

相关文章:

python - 如何比较一个文件中的多行并输出组合条目

eclipse - Eclipse 中的 Perl 语法检查,忽略丢失的模块

Perl:将Unicode字符串打印到Windows控制台

python - 从字符串中提取数字的最 Pythonic 方法

regex - 我如何将正则表达式与Pig一起使用来从数组元组中分隔数据

c++ - 为什么正则表达式在 C++ 的日语字符串中找不到 "("?

perl - 无法访问 perl 脚本中的 shell 变量,除非它是环境变量

perl - 如何优化这个 Perl 文件查找?

javascript - 如何使用Jquery分割字符串

python - 使用 re.match() 查找子字符串