my $sep = chr(033); my $str = join $sep, @files; if ($str =~ /^([^$sep]*)[^$sep]*($sep\1[^$sep]*)*\z/s) { print "Common substring: '$1'\n"; } else { die "Pattern unexpectedly failed to match?"; }