通过重新格式化现有文件名来重命名文件-与-replace运算符一起使用的替换字符串中的占位符 [英] Renaming files by reformatting existing filenames - placeholders in replacement strings used with the -replace operator

查看:84
本文介绍了通过重新格式化现有文件名来重命名文件-与-replace运算符一起使用的替换字符串中的占位符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些这样的视频文件: VideoName_s01e01.mp4,其中季节和情节是变量.我想在s??e??之间添加下划线(_).

I have a few video file like this: VideoName_s01e01.mp4 where the season and episodes are variables. I want to add an underscore ("_") between the s?? and e??.

我一直在使用的问题进行重命名,我有一个起点:

I have been using powershell for renaming purposes, I have a starting point:

GCI $path -filter '*_s??e??*' -rec |  Ren -new { $_.name -replace '_s[0-9][0-9]', '_s[0-9]_[0-9]_' } -passthru    

这实际上重命名了我的文件VideoName_s[0-9]_e[0-9].mp4.

This actually renamed my files VideoName_s[0-9]_e[0-9].mp4.

基本上,我正在寻找字符s??e??,我只是不知道如何在替换部分中使它们成为变量.

Basically, I am looking for the characters s??e?? I just don't know how to make them variables in the replace section.

我认为最好的方法是:

  1. 找到e??s??的位置(我们称它为X).
  2. X-3处分割字符串.
  3. 将字符串与中间的"_"连接起来.
  1. Find the position of e??s?? (let's call it X).
  2. split the string at X-3.
  3. concatenate the string with a "_" in the middle.

推荐答案

Martin Brandl的回答提供了一种优雅而有效的解决方案 ,但值得深入研究:

Martin Brandl's answer provides an elegant and effective solution, but it's worth digging deeper:

PowerShell的-replace运算符(... -replace <search>[, <replace>]):

PowerShell's -replace operator (... -replace <search>[, <replace>]):

  • 采用 正则表达式 作为其第一个操作数<search>(搜索表达式),并且始终匹配 global ,即,它替换 all 匹配项

  • Takes a regular expression as its first operand, <search> (the search expression), and invariably matches globally, i.e., it replaces all matches.

  • 'bar' -replace '[ra]', '@'-> 'b@@'
  • 'bar' -replace '[ra]', '@' -> 'b@@'

指定替换表达式<replace>可选,在这种情况下,空字符串将替换为<search>匹配的内容,从而导致有效的删除.

Specifying a replacement expression, <replace>, is optional, in which case the empty string is substituted for what <search> matched, resulting in its effective removal.

  • 'bar' -replace '[ra]'-> 'b'
  • 'bar' -replace '[ra]' -> 'b'

如果指定了<replace> ,则支持两种形式:

If <replace> is specified, it supports two forms:

  • v6.1 + (仅适用于PowerShell Core ):一个 script块({ ... })作为替换操作数,可以在每次匹配的基础上完全动态地计算替换字符串-例如,请参见此答案.

  • v6.1+ (PowerShell Core only): A script block ({ ... }) as the replacement operand, which offers fully dynamic calculation of the replacement string on a per-match basis - see this answer for an example.

  • 'bar' -replace '[ra]', { '{' + $_.Value + '}' }-> 'b{a}{r}'
  • 'bar' -replace '[ra]', { '{' + $_.Value + '}' } -> 'b{a}{r}'

一个字符串,其中包含一个表达式,该表达式可以引用正则表达式捕获(和未捕获)的内容,

A string containing an expression that can reference what the regular expression captured (and didn't capture), explained below.

  • 'bar' -replace '[ra]', '{$&}'-> 'b{a}{r}'
  • 'bar' -replace '[ra]', '{$&}' -> 'b{a}{r}'

-replace不区分大小写匹配- (也可以写为-ireplace);要执行区分大小写的 匹配,请使用-creplace形式.

-replace matches case-insensitively (and can also be written as -ireplace); to perform case-sensitive matching, use the form -creplace.

替换语言"用于在字符串类型的<replace>操作数中引用正则表达式捕获的对象本身不是正则表达式-此处不存在匹配项,仅引用结果 >支持的正则表达式匹配.

The "replacement language" for referencing regex captures in a string-typed <replace> operand is itself not a regular expression - no matching happens there, only references to the results of the regex matching are supported.

值得注意的是,

  • PowerShell不会在其

  • PowerShell does not explain the syntax of replacement strings in its Get-Help about_comparison_operators help topic,

,但可以在正则表达式中的替换 .NET框架帮助主题,因为PowerShell的-replace使用

but the information can be found in the Substitutions in Regular Expressions .NET framework help topic, because PowerShell's -replace uses the Regex.Replace() method behind the scenes.

为方便起见,这是<replace>字符串中受支持的引用(摘录自上面链接的页面,并添加了重点和注释):

For convenience, here are the references supported in the <replace> string (excerpted from the page linked above, with emphasis and annotations added):

  • $number(例如,$1)...包括由基于1number 标识的捕获组匹配的最后一个子字符串,其中数字是替换字符串中的十进制值.

  • $number (e.g., $1) ... Includes the last substring matched by the capture group that is identified by the 1-based number, where number is a decimal value, in the replacement string.

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆