powershell - Powershell。替换正则表达式

标签 powershell replace

RegEx for Replace正在踢我的屁股。我正在尝试找到:

value="COM8"/>



在文本文件中,将“COM8”替换为另一个com端口(即“COM9”,“COM13”等)。
(Get-Content 'C:\Path\File.config').Replace('/".*?"', '"COM99"') | Set-Content 'C:\Path\File.config'

预先感谢您提供的任何帮助。

最佳答案

Get-Content产生一个字符串列表。通过member enumeration在每个字符串上调用Replace()。意思是,您在调用 String.Replace() 方法,而不是 Regex.Replace() 方法。前者仅执行普通的字符串替换。

请使用 -replace 运算符:

(Get-Content 'C:\Path\File.config') -replace '=".*?"', '="COM99"' |
    Set-Content 'C:\Path\File.config'

关于powershell - Powershell。替换正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40032060/

相关文章:

powershell - 如何根据 powershell 中对象的两个属性选择唯一对象?

xml - powershell xml xpath

javascript - 关于 javascript 的问题 - string.replace()

python - 如果 Pandas 系列中包含列表中的单词,则替换部分字符串的最快方法

replace - 在不更改过期时间的情况下替换 memcached 中的项目

powershell - 如何在 PowerShell 上解码字符串

json - 如何将此 JSON 往返传输到 PSObject 并返回到 Powershell

powershell - 如何替换字符串/文件末尾的字符?

string - 从 f# 中的字符串中删除字符

python - 如何使用字典值在 Python 中进行正则表达式替换,其中键是来自同一字符串的另一个匹配对象