Regex Powershell:使用内联计算替换正则表达式

Regex Powershell:使用内联计算替换正则表达式,regex,powershell,Regex,Powershell,我用yyyyym命名文件,我需要用mmyyyy重命名此文件,但新名称中的mm必须小于一个文件。我需要在更换过程中在线完成 我可以用正则表达式替换带模式的字符串abc217 'abc201706.txt'-替换“^(.*)([0-9]{4})([0-9]{2})(\..*)$”,“$1$3$2$4” 如何将正则表达式替换为abc052017.txt? 换句话说,我需要从$3中内联减去一个月,这意味着第一个月 我已经在很多帖子中寻找了我问题的答案,但是没有结果。请不要把我的问题标为重复问题并回答它

我用
yyyyym
命名文件,我需要用
mmyyyy
重命名此文件,但新名称中的
mm
必须小于一个文件。我需要在更换过程中在线完成

我可以用正则表达式替换带模式的字符串abc217

'abc201706.txt'-替换“^(.*)([0-9]{4})([0-9]{2})(\..*)$”,“$1$3$2$4”

如何将正则表达式替换为
abc
05
2017.txt

换句话说,我需要从
$3
中内联减去一个月,这意味着第一个月


我已经在很多帖子中寻找了我问题的答案,但是没有结果。请不要把我的问题标为重复问题并回答它

没有美丽,但应该有用:

$myfilepath = 'C:\temp\abc2017 06.txt'
$file = Get-Item $myFilePath -Force 
$basename = $file.basename

$basename -match '^(?<name>\D+)(?<year>\d{4})\s(?<month>\d{2})'

$dateString = "{0}/{1}/01" -f $matches.year, $matches.month
$Datetime = $dateString | Get-Date
$Datetime = $Datetime.AddMonths(-1)

$newBasename = "{0} {1} {2}{3}" -f $matches.name, $Datetime.ToString('MM'), $Datetime.ToString('yyyy'), $file.Extension
$myfilepath='C:\temp\abc217 06.txt'
$file=获取项$myFilePath-Force
$basename=$file.basename
$basename-匹配“^(?\D+)(\D{4})\s(?\D{2}”
$dateString=“{0}/{1}/01”-f$matches.year,$matches.month
$Datetime=$dateString |获取日期
$Datetime=$Datetime.AddMonths(-1)
$newBasename=“{0}{1}{2}{3}”-f$matches.name、$Datetime.ToString('MM')、$Datetime.ToString('yyyy')、$file.Extension
更新:您的正则表达式不匹配,我做了一些更改

更新2:将其写入文件,并根据需要调用它

param(
    [string]$myFilePath
)
$file = Get-Item $myFilePath -Force 
$basename = $file.basename

$null = $basename -match '^(?<name>\D+)(?<year>\d{4})\s(?<month>\d{2})'

$dateString = "{0}/{1}/01" -f $matches.year, $matches.month
$Datetime = $dateString | Get-Date
$Datetime = $Datetime.AddMonths(-1)

$newFilename = "{0} {1} {2}{3}" -f $matches.name, $Datetime.ToString('MM'), $Datetime.ToString('yyyy'), $file.Extension

return $newFilename
param(
[字符串]$myFilePath
)
$file=获取项$myFilePath-Force
$basename=$file.basename
$null=$basename-match'^(?\D+(\D{4})\s(?\D{2})'
$dateString=“{0}/{1}/01”-f$matches.year,$matches.month
$Datetime=$dateString |获取日期
$Datetime=$Datetime.AddMonths(-1)
$newFilename=“{0}{1}{2}{3}”-f$matches.name、$Datetime.ToString('MM')、$Datetime.ToString('yyyy')、$file.Extension
返回$newFilename

e、 g.:ConvertFilename.ps1-myFilePath“c:\Blah”

一种方法是使用.NET正则表达式,基本上是可以计算替换值的回调函数

Powershell脚本块可用作匹配计算器。它们接收匹配对象作为第一个参数,它们产生的值将用作替换

Get-ChildItem -Filter "*.txt" | Foreach-Object {
    $_.Name = [Regex]::Replace($_.Name, "(\d{6})(\.txt)$", {
        $match = $args[0]
        try {
            [DateTime]::ParseExact($match.Groups[1], "yyyyMM", $null).AddMonths(-1).ToString("MMyyyy") + $match.Groups[2]
        } catch {
            $match
        }
    })
}
try块尝试将匹配组1中的六位数字解析为日期时间,减去一个月,然后将所有内容重新组合在一起


catch块只输出原始匹配,以防转换为DateTime失败。

我会在几行中完成,可能没有替换。只需
-匹配
基本名称
,利用
$matches
进行计算。在那之后,用
-f
操作符再次构建basename字符串。这不是我需要的,但是你能把示例放在一行中吗?酷,但我需要把它放在一行中。。。但这将是可笑的漫长。为什么你需要它作为一个单一的?作为一个函数来实现它怎么样?哈,当然,我可以把代码扩展到一行,但它绝对不可读)我需要一行代码,因为我必须从另一个脚本调用它,它必须看起来像这样的模式文件名模式不同?