[Israel.pm] Regexp
Mikhael Goikhman
migo at homemail.com
Thu Jun 2 01:53:48 PDT 2005
On 02 Jun 2005 11:05:05 +0300, Peter Gordon wrote:
>
> Suppose I have the text "HarryMaryJaneHarryxMaryJane" and I want to find
> the longest duplicate match.
>
> If I use /(.+).*?(\1)/
> it matches Harry, but the longest match is MaryJane.
>
> What is the best approach to this problem?
Without thinking too much on this problem, I would solve it like this:
my $string = "HarryMaryJaneHarryxMaryJane";
my $longest_substr = "";
while (length $string) {
$longest_substr = $1
if $string =~ /(.+).*?\1/s
&& length($1) > length($longest_substr);
$string =~ s/^.//s;
}
print "$longest_substr\n";
And if you want to make it a bit more efficient, replace
$string =~ /(.+).*?\1/s
with
$string =~ s/^.*?(.+).*?\1/$&$`/s
Regards,
Mikhael.
--
perl -e 'print+chr(64+hex)for+split//,d9b815c07f9b8d1e'
More information about the Perl
mailing list