使用通配符将文件中的文本替换到行尾

时间:2018-10-30 16:17:07

标签: powershell

我正在尝试替换文件中的字符串。示例文本文件File.txt

<add key="apple" value="C:\test\web.config"/>
<add key="banana" value="C:\test\web2.config"/>
<add key="orange" value="C:\test\web3.config"/>

这是我到目前为止使用通配符匹配的代码:

$content = Get-Content -Path C:\File.txt
$content -replace "<add key=`"apple*", '<add key="mango" value="C:\test\web.config"/>'

输出:

<add key="mango" value="C:\test\web.config"/>" value="C:\test\web.config"/>

所需的输出:

<add key="mango" value="C:\test\web.config"/>

通配符*与行尾不匹配。

我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

作为正则表达式,也许您需要一个public class UserViewModel : User { public Car car { get; set; } } public class User { [Required] public string Name { get; set; } } 而不是@model Project.Models.UserViewModel <p>@Model.User</p> 吗?

我要说的是,通过使用apple.*来告诉正则表达式为“ appl”,之后再根据需要输入尽可能多的“ e”