首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在-replace运算符中使用变量?

如何在-replace运算符中使用变量?
EN

Stack Overflow用户
提问于 2022-08-01 19:32:39
回答 1查看 46关注 0票数 1

我对powershell相当陌生,我一直在尝试创建一个脚本,该脚本将标题中包含数字的目录中的所有文件重新命名为模板。

我的剧本看起来是这样的:

代码语言:javascript
复制
$NewObjName = "My New Name"

cd E:\Test

dir | ren -NewName {$_.name -replace '([a-zA-Z])([a-zA-Z])\.(\d?)(\d).*','0$3$4'}
dir | ren -NewName {$_.name -replace '(\d?)(\d)(\d)',{Part $2$3 $NewObjName.txt}}

做这件事:

代码语言:javascript
复制
PS E:\Test> ls


    Directory: E:\Test


Mode                 LastWriteTime         Length Name                                                                                                                                            
----                 -------------         ------ ----                                                                                                                                            
-a----          8/1/2022   2:14 PM              0 AB.01CDE                                                                                                                                        
-a----          8/1/2022   2:15 PM              0 AB.02CDE                                                                                                                                        
-a----          8/1/2022   2:15 PM              0 AB.10CDE                                                                                                                                        
-a----          8/1/2022   2:15 PM              0 AB.11CDE                                                                                                                                        



PS E:\Test> E:\RenameScript.ps1

PS E:\Test> ls


    Directory: E:\Test


Mode                 LastWriteTime         Length Name                                                                                                                                            
----                 -------------         ------ ----                                                                                                                                            
-a----          8/1/2022   2:14 PM              0 Part 01 $NewObjName.txt                                                                                                                         
-a----          8/1/2022   2:15 PM              0 Part 02 $NewObjName.txt                                                                                                                         
-a----          8/1/2022   2:15 PM              0 Part 10 $NewObjName.txt                                                                                                                         
-a----          8/1/2022   2:15 PM              0 Part 11 $NewObjName.txt                                                                                                                         



PS E:\Test> 

所以它只是放入变量名,而不是变量的值。我不确定-replace是不允许正则表达式分组以外的变量,还是只是遗漏了什么?

(预先谢谢:)

编辑:我的预期结果如下:

代码语言:javascript
复制
PS E:\Test> ls


    Directory: E:\Test


Mode                 LastWriteTime         Length Name                                                                                                                                            
----                 -------------         ------ ----                                                                                                                                            
-a----          8/1/2022   2:14 PM              0 Part 01 My New Name.txt                                                                                                                         
-a----          8/1/2022   2:15 PM              0 Part 02 My New Name.txt                                                                                                                         
-a----          8/1/2022   2:15 PM              0 Part 10 My New Name.txt                                                                                                                         
-a----          8/1/2022   2:15 PM              0 Part 11 My New Name.txt    
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-01 19:58:54

$_.name -replace '(\d?)(\d)(\d)',{Part $2$3 $NewObjName.txt}

代码语言:javascript
复制
- When a script block is used in a context where it is coerced to a string, such as in your case, its _verbatim content_ (sans `{` and `}`) is used. Therefore, `$NewObjName` was _not_ expanded (interpolated).
代码语言:javascript
复制
    - While it can be convenient to use `{ ... }` in lieu of an actual string literal in order to avoid the need for escaping of embedded quoting, it is best avoided in the interest of conceptual clarity.
代码语言:javascript
复制
- As an aside: In _PowerShell (Core) 6.1+_, script blocks _are_ now supported as the replacement operand of the [`-replace`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Comparison_Operators#replacement-operator) operator, albeit _as script blocks_, i.e. they provide a piece of code to run for a each match in order to dynamically determine the replacement text - see [this answer](https://stackoverflow.com/a/58608437/45375) for an example.
  • 由于需要字符串展开(插值),所以必须将替换操作数指定为)
代码语言:javascript
复制
- However, `$2` and `$3` - even tough they look like PowerShell variables - are actually placeholders for what the regex operation matched, and therefore must be _passed through_ to the .NET regex engine.
代码语言:javascript
复制
- To that end (to prevent up-front expansion by PowerShell), they must be escaped as ``$2` and ``$3`, using ```, the so-called _backtick_, PowerShell's [escape character](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Special_Characters).
代码语言:javascript
复制
- Note:
代码语言:javascript
复制
    - If you use a [verbatim (single-quoted) string (`'...'`)](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Quoting_Rules#single-quoted-strings) - which is the best choice if interpolation is _not_ needed - placeholders such as `$2` and `$3` can be used as-is.
代码语言:javascript
复制
    - As an alternative to the expandable string solution below, you can conceptually separate the aspect of up-front expansion and the verbatim string to pass to the .NET regex engine via `-f`, the [format operator](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Operators#format-operator--f):

与:“$2$3 $NewObjName.txt”('Part 2$3$ {0}.txt‘-f $NewObjName)相同

因此:

代码语言:javascript
复制
$_.name -replace '(\d?)(\d)(\d)', "Part `$2`$3 $NewObjName.txt"
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73198710

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档