Regex 如何使用windows powershell正则表达式删除文件名末尾的一个或多个空格?

Regex 如何使用windows powershell正则表达式删除文件名末尾的一个或多个空格?,regex,windows,powershell,scripting,Regex,Windows,Powershell,Scripting,我有一个目录,其中包含许多文件名格式错误的文件。其中一些文件名的末尾确实有“空格”。另一些在文件名字符串末尾的文件名中有一些关键字。例如“xxx xxx xxx somewordEng.txt” 我试图摆脱他们使用这个脚本,但它还不行。文件名(Basename)末尾的空格仍然存在,因此“Eng”关键字以某种方式添加到单词之前: dir | Rename-Item -NewName { $_.BaseName.replace("Eng$","").replace(" {2,}"," ").repl

我有一个目录,其中包含许多文件名格式错误的文件。其中一些文件名的末尾确实有“空格”。另一些在文件名字符串末尾的文件名中有一些关键字。例如“xxx xxx xxx somewordEng.txt”

我试图摆脱他们使用这个脚本,但它还不行。文件名(Basename)末尾的空格仍然存在,因此“Eng”关键字以某种方式添加到单词之前:

dir | Rename-Item -NewName { $_.BaseName.replace("Eng$","").replace(" {2,}"," ").replace("\s$","") + $_.Extension }

.replace("Eng$","")  is supposed to remove the "Eng" keyword if it appears at the END of the filename (basename), seems not working so far.

.replace(" {2,}"," ")   is supposed to replace 2 or more following spaces with just ONE space within the filename, seems not working so far.

.replace("\s$","")    is supposed to remove spaces at the end of the filename, does not work neither. 

我搜索了powershell正则表达式示例,但到目前为止似乎没有任何结果(还看不出问题。

这里的问题是字符串方法
.Replace()
不支持正则表达式,这正是您在这里尝试执行的操作。您应该改用Replace运算符
-Replace
。这两个选项之间的差异将在中详细介绍

以下两个示例显示了这种差异

PS C:\Users\mcameron> "Te.t".Replace(".","s")
Test

PS C:\Users\mcameron> "Te.t" -Replace ".","s"
ssss 
在您的情况下

$_.BaseName -replace "Eng$" -replace " {2,}"," " -replace "\s$"
我们使用了正确的运算符,您仍然可以像上面所看到的那样“链接”它们。这将删除尾随词“Eng”和任何尾随单个空格。以及将一组空格减少为单个空格。此外,如果您要替换为nothing,则可以忽略第二个参数

然而,如果你想的话,你可以把它们收紧一点

$_.BaseName -replace "(Eng|\s+)$" -replace "\s{2,}"," "

这里的问题是字符串方法
.Replace()
不支持正则表达式,这正是您在这里尝试执行的操作。您应该改用Replace运算符
-Replace
。这两个选项之间的差异将在中详细介绍

以下两个示例显示了这种差异

PS C:\Users\mcameron> "Te.t".Replace(".","s")
Test

PS C:\Users\mcameron> "Te.t" -Replace ".","s"
ssss 
在您的情况下

$_.BaseName -replace "Eng$" -replace " {2,}"," " -replace "\s$"
我们使用了正确的运算符,您仍然可以像上面所看到的那样“链接”它们。这将删除尾随词“Eng”和任何尾随单个空格。以及将一组空格减少为单个空格。此外,如果您要替换为nothing,则可以忽略第二个参数

然而,如果你想的话,你可以把它们收紧一点

$_.BaseName -replace "(Eng|\s+)$" -replace "\s{2,}"," "

.replace
不是
-replace
后者支持正则表达式。前者是简单的通配符。我想补充一点,您完全可以使用
.Trim()
方法来执行此操作。
.replace
不是
-replace
后者支持正则表达式。前者是简单的通配符。我想补充一点,您完全可以使用
.Trim()
方法来执行此操作。嗯……好的,谢谢。但是我如何将这两种方法组合成一行?我尝试了您使用“-replace regex”的建议与“.replace()”方法结合使用,但atm仍然失败(或只是部分工作)。请参见以下示例:dir | Rename Item-NewName{$\u.BaseName.replace(“nastyKeyword1”),replace(“nastyKeyword1”,“”)-replace“Eng$”-replace“{2,}”,“replace”\s$“+$.Extension}”插入到那个位置会给我带来一个错误。但是我希望在那个位置进行替换,这样我就不必关心扩展。我如何修复这个问题?或者我可以做一些简单的事情{{{$}.BaseName-replace“(Eng |\s+)$”-replace“\s{2,},”.replace(“nastyKeyword1,”).replace(“nastyKeyword2,”),“)}}不知怎的???@AxelWerner没有像那样嵌套花括号。它们表示一个脚本块。你应该能够试验你当前的逻辑,但使用
()
。你也可以
-很快替换“NastyKeyword1 | NastyKeyword2 |”
hmmm..好的,谢谢。但是我如何才能将这两个方法组合成一行呢?我尝试了你的建议,将“-replace regex”与“.replace()”方法结合使用,但还是失败了(或者只是部分工作)。请参见下面的示例:dir | Rename Item-NewName{$\uuu.BaseName.replace(“nastyKeyword1”,”).replace(“nastyKeyword1”,”).replace(“nastyKeyword1”,”)-replace“Eng$”-replace“{2,}”,插入该位置的-replace“\s$”+$扩展名}会抛出一个错误。但是我希望在该位置进行替换,这样我就不必关心扩展名了。我如何修复该问题?或者我可以做一些细线{{$\.BaseName-replace“(Eng|\s+$”-replace“\s{2,}”,replace.replace(“nastyKeyword1”,”).replace(“nastyKeyword2”,”)}}}不知何故@AxelWerner不会像那样嵌套花括号。它们表示一个脚本块。您应该能够试验您当前的逻辑,但可以使用
()
。您也可以只
-替换“nastyKeyword1 | nastyKeyword2 |并尽快”