Powershell .Replace 正则表达式 [英] Powershell .Replace RegEx

查看:75
本文介绍了Powershell .Replace 正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

RegEx for Replace 让我大吃一惊.我正在尝试查找:

RegEx for Replace is kicking my butt. I am trying find:

value="COM8"/>

value="COM8"/>

在一个文本文件中,将COM8"替换为另一个 com 端口(即COM9"、COM13"等).

in a text file and replace "COM8" with another com port (ie "COM9", "COM13", etc).

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

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

Thank you in advance for any help you can provide.

推荐答案

Get-Content 生成字符串列表.Replace() 通过 成员枚举.意思是,您正在调用 String.Replace() 方法,而不是 Regex.Replace() 方法.前者只进行普通的字符串替换.

Get-Content produces a list of strings. Replace() is called on each string via member enumeration. Meaning, you're calling the String.Replace() method, not the Regex.Replace() method. The former does only normal string replacements.

使用 -replace 运算符相反:

Use the -replace operator instead:

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

这篇关于Powershell .Replace 正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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