Perl 以条件形式将输入置于多个正则表达式中?

Perl 以条件形式将输入置于多个正则表达式中?,perl,Perl,我在这里得到了大量的信息,让一系列核苷酸搜索3个核苷酸的重复模式,并要求通过为其构造正则表达式,重复连续发生7次 my $regex1 = qr/( ([ACGT]{3}) \2{6,} )/x; 我知道如何扩展它,在10行中搜索2个NUC,以及在7行中搜索4个NUC 但是我想扩展代码,这样用户可以指向他们的输入文件,它会检查上面的正则表达式以及我需要为另外两个搜索创建的另外两个正则表达式 编辑:如何使我的输入文件受制于多个正则表达式,如上面的一个?我在代码中创建了另外两个正则表达式(被散列符

我在这里得到了大量的信息,让一系列核苷酸搜索3个核苷酸的重复模式,并要求通过为其构造正则表达式,重复连续发生7次

my $regex1 = qr/( ([ACGT]{3}) \2{6,} )/x;
我知道如何扩展它,在10行中搜索2个NUC,以及在7行中搜索4个NUC

但是我想扩展代码,这样用户可以指向他们的输入文件,它会检查上面的正则表达式以及我需要为另外两个搜索创建的另外两个正则表达式

编辑:如何使我的输入文件受制于多个正则表达式,如上面的一个?我在代码中创建了另外两个正则表达式(被散列符号删除)

这是我目前的代码

print "Please specify the file location (DO NOT DRAG/DROP files!) then press ENTER:\n";
$seq = <STDIN>;

#Remove the newline from the filename
chomp $seq;

#open the file or exit
open (SEQFILE, $seq) or die "Can't open '$seq': $!";

#read the dna sequence from the file and store it into the array variable @seq1
@seq1 = <SEQFILE>;

#Close the file
close SEQFILE;

#Put the sequence into a single string as it is easier to search for the motif
$seq1 = join( '', @seq1);

#Remove whitespace
$seq1 =~s/\s//g;

#Count of number of nucleotides
#Initialize the variable
$number = 0;
$number = length $seq1;

#Use regex to say "Find 3 nucelotides and match at least 6 times
# qr(quotes and compiles)/( ([nucs]{number of nucs in pattern}) \2{number of repeats,}/x(permit within pattern)
my $regex1 = qr/( ([ACGT]{3}) \2{6,} )/x;
#my $regex = qr/( ([ACGT]){2}) \2{9,} )/x;
#my $regex2 = qr/( ([ACGT]{4}) \2{6,} )/x;

#Tell program to use $regex on variable that holds the file
$seq1 =~ $regex1;

#Now print the results to screen
#This will need to change to printing to a file (WHAT KIND OF FILE?)in the following manner :site, nucelotide match, # of times, length of full sequence
printf "MATCHED %s exactly %d times\n", $2, length($1)/3;
print "Length of sequence: $number\n";
exit; 
print“请指定文件位置(不要拖放文件!),然后按ENTER键:\n”;
$seq=;
#从文件名中删除换行符
chomp$seq;
#打开文件或退出
打开(SEQFILE,$seq)或死亡“无法打开'$seq':$!”;
#从文件中读取dna序列并将其存储到数组变量@seq1中
@seq1=;
#关闭文件
关闭SEQFILE;
#将序列放在一个字符串中,因为更容易搜索主题
$seq1=连接(“”,@seq1);
#删除空白
$seq1=~s/\s//g;
#核苷酸计数
#初始化变量
$number=0;
$number=长度$seq1;
#使用正则表达式说“找到3个nucelotides并匹配至少6次
#qr(引用和编译)/([nucs]{模式中的NUC数})\2{重复次数,}/x(模式中的许可)
my$regex1=qr/([ACGT]{3})\2{6,})/x;
#my$regex=qr/([ACGT]){2})\2{9,})/x;
#my$regex2=qr/([ACGT]{4})\2{6,})/x;
#告诉程序在保存文件的变量上使用$regex
$seq1=~$regex1;
#现在将结果打印到屏幕上
#这将需要更改为按以下方式打印文件(什么类型的文件?):站点、nucelotide匹配、#次、完整序列长度
printf“精确匹配%s%d次\n”,$2,长度($1)/3;
打印“序列长度:$number\n”;
出口

只需为循环使用一个

for my $regex ($regex1, $regex2, $regex3) {
  next unless $seq1 =~ $regex;
  printf "MATCHED %s exactly %d times\n", $2, length($1)/length($2);
}

但您可能希望更改输出以更好地描述结果。

您的问题是什么?抱歉-我不清楚。我想将输入文件置于多个正则表达式中。我需要更改“长度($1)”/n、 ,但如何将使用的正则表达式与它应该使用的printf进行匹配?如果不知道您想要什么输出,很难给出任何建议。但是您可以将表达式更改为
length($1)/length($2)
。答案相应更改。由于输入seq仅满足三个正则表达式要求中的一个,因此我需要解决未找到任何内容的正则表达式,这些正则表达式返回
长度($2)
as
0
,这会终止程序。我如何告诉程序只提供它使用的正则表达式的结果?答案也已修复。还有其他问题吗?:)没有!我想我可以让它工作。你帮了大忙!