Regex PowerShell将字符串与正则表达式匹配

Regex PowerShell将字符串与正则表达式匹配,regex,powershell,Regex,Powershell,我正在编写一个脚本,将电视节目移动到硬盘上相应的文件夹中。我在将节目与他们的文件夹匹配时遇到问题。这是我遇到问题的代码片段: #Remove all non-alphanumeric characters from the name $newname = $Episode.Name -replace '[^0-9a-zA-Z ]', ' ' #Split the name at S01E01 and store the showname in a variable (Text before

我正在编写一个脚本,将电视节目移动到硬盘上相应的文件夹中。我在将节目与他们的文件夹匹配时遇到问题。这是我遇到问题的代码片段:

#Remove all non-alphanumeric characters from the name
$newname = $Episode.Name -replace '[^0-9a-zA-Z ]', ' '  

#Split the name at S01E01 and store the showname in a variable (Text before S01E01) 
$ShowName = [regex]::Split($newname, 'S*(\d{1,2})(x|E)')[0]

#Match and get the destination folder where the names are similar
################## THIS IS WHERE THE ISSUE IS #######################
$DestDir = Gci -Path $DestinationRoot | Where { $ShowName -like "*$($_.Name)*" } | foreach {$_.Name }
例如,一个名为“Doctor Who 2005 S02E02 Total and Claw.mp4”的节目没有返回一个名为“Doctor Who”的类似文件夹

问题: 谁可以修改$DestDir以便匹配名称?有更好的方法吗

工作代码:

# Extract the name of the show (text before SxxExx)
$ShowName = [regex]::Split($Episode.Basename, '.(\d{1,3})(X|x|E|e)(\d{1,3})')[0]

# Assumption: There is a folder in TV shows directory that is named correctly, and the input file is named correctly
# Try to match by stripping all non-Alphabet characters from both names and check if the folder name contains the file name
$Folder = gci -Path $DestinationRoot | 
          Where {$_.PSisContainer -and `
          (($_.Name -replace '[^A-Za-z]','') -match ($ShowName -replace '[^A-Za-z]','')) } |
          select -ExpandProperty fullname
测试的一些样本输出:

Input file name:   Arrow S01E02.mp4
Show name:         Arrow 
Matching folder:   C:\Users\Public\Videos\TV Shows\Arrow
-----------------------------------------------------------------------
Input file name:   Big Bang Theory S3E03.avi
Show name:         Big Bang Theory 
Matching folder:   C:\Users\Public\Videos\TV Shows\The Big Bang Theory
-----------------------------------------------------------------------
Input file name:   Doctor Who S08E03.mp4
Show name:         Doctor Who 
Matching folder:   C:\Users\Public\Videos\TV Shows\Doctor Who (2005)
-----------------------------------------------------------------------
Input file name:   GameOfThronesS01E01.mp4
Show name:         GameOfThrones
Matching folder:   C:\Users\Public\Videos\TV Shows\Game Of Thrones
-----------------------------------------------------------------------

使用与您相同的方法根据您的建议确定节目名称。与Who 2005 S02E02牙齿和爪子医生合作。mp4

$showName = $Episode -replace '[^0-9a-zA-Z ]'
$showName = ($showName -split ('S*(\d{1,2})(x|E)'))[0]
$showName = $showName -replace "\d"
我添加了一行
$showName=$showName-replace“\d”
,以说明季节中的年份。有一个警告,如果节目中包含一个数字,但应该为大多数工作。继续执行
$DestDir
确定。问题的一部分是您的
Where
比较向后。您希望查看显示名称是否是潜在文件夹的一部分,而不是相反。此外,由于潜在文件夹可能包含空格,因此comaparison也应包含该假设

Get-ChildItem -Path $DestinationRoot -Directory  | Where-Object { ($_.name -replace " ") -like "*$($showName)*"}

我会继续使用一个选项让用户确认文件夹,因为它可能有多个匹配项。我想指出的是,可能很难解释所有命名约定和差异,但您已经有了一个良好的开端

恕我直言,但为什么不确保文件夹与节目名称匹配,即将“DoctorWho”重命名为“DoctorWho 2005”?PowerShell不知道您的“相似”概念是什么,您必须详细说明每个案例。您可以做的一件事是添加
.Trim()
,这样就可以删除开头和结尾的空格,但您必须明确说明进一步的转换。@Jeroenmoster在我的情况下可以这样做。然而,我计划在完成后与其他人分享。我不确定它们的设置是什么样的,所以我试着让它尽可能通用。好吧,你可以做一些事情,比如“去掉show name和folder name中的所有空格和尾随数字,然后开始匹配”,但似乎不管你做什么,都有人会抱怨它工作不正常。至少简单的算法有一个好处,人们可以理解正在发生的事情。我最后做了一些类似于你所建议的事情。我做了一个包含,而不是一个类似的。至于选择,我已经决定脚本将只发送电子邮件给用户,让他们知道存在不匹配的文件,这将需要手动干预。谢谢你的帮助@Kevin \
-包含
仅适用于阵列。在
的where object
中,您将不会使用数组。FYIWhoops,意思是说-匹配。