我正在尝试将我的文件重命名"Introduction _ C# _ Tutorial 1"
为"01.Introduction"
. 它需要一个-replace
运算符以及一个-f
运算符来对索引号进行零填充。我的代码是这样的:
$string = "Introduction _ C# _ Tutorial 1"
if ($string -match "^([^_]+)_[^\d]+(\d{1,2})$") {
$Matches[0] -replace "^([^_]+) _[^\d]+(\d{1,2})$", ("{0:d2}. {1}" -f '$2', '$1')
}
然而,输出就像-f
操作员缺席一样:
1. Introduction
我怎样才能得到预期的结果?
顺便说一句,有没有一种简单的方法可以在$matches
没有语句的情况下获得结果-match
,或者将-match
语句组合成单行代码?