Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Longest common substring for more than two strings in PowerShell?

Tags:

powershell

how can I find the matching strings in an array of strings in PowerShell:

example:

$Arr = "1 string first",
"2 string second",
"3 string third",
"4 string fourth"

Using this example, I want this returned:

" string "

I want to use this to find matching parts of file names and then remove that part of the file name (like removing the artist's name from a set of mp3 files for example), without having to specify which part of the file name should be replaced manually.

like image 990
Winfred Avatar asked Nov 20 '11 02:11

Winfred


1 Answers

$arr =  "qdfbsqds", "fbsqdt", "bsqda" 
$arr | %{

$substr = for ($s = 0; $s -lt $_.length; $s++) {
           for ($l = 1; $l -le ($_.length - $s); $l++) {
            $_.substring($s, $l);
           }
          } 
$substr | %{$_.toLower()} | select -unique

} | group | ?{$_.count -eq $arr.length} | sort {$_.name.length} | select -expand name -l 1
# returns bsqd
  • produce a list of all the unique substrings of the inputstrings
  • filter for substrings that occur #inputstrings times (i.e. in all input strings)
  • sort these filtered substrings based on the length of the substring
  • return the last (i.e. longest) of this list
like image 137
jon Z Avatar answered Oct 14 '22 02:10

jon Z