以下は、文字列中の連続した部分文字列を見つけるためのスクリプトです。文字列とレコードの場所に複数の部分文字列を見つける
use strict;
use warnings;
my $file="Sample.txt";
open(DAT, $file) || die("Could not open file!");
#worry about these later
#my $regexp1 = "motif1";
#my $regexp2 = "motif2";
#my $regexp3 = "motif3";
#my $regexp4 = "motif4";
my $sequence;
while (my $line = <DAT>) {
if ($line=~ /(HDWFLSFKD)/g){
{
print "its found index location: ",
pos($line), "-", pos($line)+length($1), "\n";
}
if ($line=~ /(HD)/g){
print "motif found and its locations is: \n";
pos($line), "-", pos($line)+length($1), "\n\n";
}
if ($line=~ /(K)/g){
print "motif found and its location is: \n";
pos($line), "-",pos($line)+length($1), "\n\n";
}
if ($line=~ /(DD)/g){
print "motif found and its location is: \n";
pos($line), "-", pos($line)+length($1), "\n\n";
}
}else {
$sequence .= $line;
print "came in else\n";
}
}
substring1とstringが一致し、substring1が一致する位置が出力されます。問題は残りの部分文字列を見つけることにあります。 substrings2の場合、substring1が見つかった位置から開始する代わりに、文字列の先頭からやり直します。問題は、位置を計算するたびに、以前に見つかった部分文字列の位置から開始するのではなく、文字列の先頭から開始することです。部分文字列は部分文字列1、部分文字列2、部分文字列3、部分文字列4であるため、その位置は前の文字列の後になければなりません。
あなたのコードには、角かっこの開閉に関する問題があるようです。これを整理するためにインデントを修正してください。 – Svante
最近のPerlの質問のほとんどは、さまざまなコンテキストで正規表現を中心に展開しています。そのような質問にすばやく答える方法を見つけることができますか? – innaM
perldoc perlretutを1〜2回読んで、必要に応じてperldoc perlrerefと相談すれば、すぐにこれらの質問のほとんどに答えが出ます。 –