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.
$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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With