in reply to Finding common substrings

Of course there's an algorithm for doing that, but it's undoubtedly overkill for your application. I think you should bring some "semantics" to the problem to make it easier. For example, both of those strings look like they contain space-separated values. So you could convert both to arrays of non-space strings, and compare the two arrays for any elements in common. One way:

$a = 'PF01389 6 218 1 255 430.09'; $b = 'PF00691 PF01389'; my @a = split ' ', $a; my @b = split ' ', $b; my %a; @a{@a} = (); my @common = grep { exists $a{$_} } @b;

Also see the Categorized Question How can I find the union/difference/intersection of two arrays?

Update: Just for fun, here's another way, using regexes:

my $pattern = $b; # so as not to bash $b $pattern =~ s/\s+/|/g; @common = " $a " =~ /\s($pattern)\s/g;

Update2: That breaks down if the string being used as the source of the regex ($b here) contains regex-special characters. Better:

my $pattern = join '|', map quotemeta($_), split ' ', $b; @common = " $a " =~ /\s($pattern)\s/g;

We're building the house of the future together.