On Mar 7, Nikola Janceski said:

>I was wondering if there is a module out there that will find a the common
>pattern among an array of strings and return the regular expression to match
>the common part.

It's not going to be "fun", but it can be done:

  sub longest_common_substr {
    # provided you know there are no NULs
    my $str = join "\0", @_;
    my $rep = @_ - 1;
    my $len = 1;
    my $match;

    $len = length($match = $1) + 1
      while $str =~ m{
        (?=
          ([^\0]{$len,})
          (?: [^\0]* \0 [^\0]*? \1 ){$rep}
        )
      }xg;

    return $match;
  }

>@stuff = qw (123crapstuff morecrap crappedshit);

  print longest_common_substr(@stuff);  # "crap"

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to