gpt4 book ai didi

regex - 使用powershell ForEach-Object匹配字符串并将其替换为正则表达式

转载 作者:行者123 更新时间:2023-12-04 13:40:59 25 4
gpt4 key购买 nike

我使用下面的管道读取文件并替换其中的一行,然后将其保存到另一个文件,但是发现目标文件中的字符串没有被替换,它仍然是旧的。

原始行是:name-1a2b3c4d

新行应为:name-6a5e4r3h

(Get-Content "test1.xml") | ForEach-Object {$_ -replace '^name-.*$', "name-6a5e4r3h"} | Set-Content "test2.xml"

那里缺少什么吗?

最佳答案

您缺少的一件事是-replace运算符在数组上可以正常工作,这意味着您根本不需要那个foreach-object循环:

(Get-Content "test1.xml") -replace '^name-.*$', 'name-6a5e4r3h' | Set-Content test2.xml

关于regex - 使用powershell ForEach-Object匹配字符串并将其替换为正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15290354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com