Perl:查找两个字符串的所有匹配子串

时间:2014-11-13 23:01:46

标签: string perl comparison substring maxlength

也许有一个函数,可以在perl中找到string1和string2的每个(最大长度)相等的子串,不是吗?

我可以使用m/substring/g;找到字符串中的每个子字符串。

为了搜索所有相等的子串,我必须移动string1的开头和逐个符号的比较字符串的指针。我怎么能在perl中做到这一点,还是有办法更容易? (准备好的功能)

提前谢谢。

my $string1 = "... (i==i)kn;i=n.n;k(i(i,"%i",&i);i ..."; my $string2 = "... k;kn;i=n.n;k;k(i(i,"%i",&i);k ..."; my @answer = ( ..., "kn;i=n.n;", "k(i(i,"%i",&i);", ... );

1 个答案:

答案 0 :(得分:1)

您的示例似乎显示返回两个不同长度的子字符串,其中较短的一个,所以我不确定“最大长度”是什么意思。但这可能有所帮助:

use Tree::Suffix;
my $string1 = '(i==i)kn;i=n.n;k(i(i,"%i",&i);i';
my $string2 = 'k;kn;i=n.n;k;k(i(i,"%i",&i);k';
my $tree = Tree::Suffix->new($string1, $string2);
my @answer;
my $min_length = 1;
my $max_length = 0; # 0 initially means no limit
do {
    my @by_length = $tree->lcs($min_length,$max_length);
    last unless @by_length;
    # don't include any substrings that are substrings of substrings already found
    for my $new_substring (@by_length) {
        push @answer, $new_substring if 0 == grep $_ =~ /\Q$new_substring/, @answer;
    }
    $max_length = length($by_length[0])-1;
} while $max_length >= $min_length;
use Data::Dumper;
print Dumper \@answer;

输出:

$VAR1 = [
      ';k(i(i,"%i",&i);',
      'kn;i=n.n;k'
    ];

Tree :: Suffix安装有点痛苦;我不得不删除包含的inc / Devel / CheckLib.pm,因为它有错误并单独安装Devel :: CheckLib,以及下载和安装libstree库。