当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell - 替换字符串中的文本 [示例]

作者:精品下载站 日期:2024-12-14 21:53:38 浏览:14 分类:玩电脑

PowerShell - 替换字符串中的文本 [示例]


在我们的日常 IT 管理任务中,我们经常需要对数据执行字符串操作以查找特定字符串、替换字符串中的文本并返回新的修改后的字符串。

PowerShell中,Replace()方法和-replace运算符用于查找指定字符并将其替换为新字符串。

使用 Replace() 方法或替换运算符,您可以在 PowerShell 中轻松替换字符串中的文本或用新文本替换字符串的特定部分或整个字符串。

在这篇文章中,我将向您解释如何执行PowerShell字符串替换操作、字符串变量替换和多个字符串替换。

使用 PowerShell Replace() 方法

PowerShell Replace() 方法用于将给定字符串中的旧字符替换为新字符或文本。

语法

.Replace(string oldvalue, string newvalue)

或者

.Replace(char oldvalue, char newvalue)

参数

oldvalue - 要查找的字符/字符串

newvalue - 用于替换的字符/字符串

让我们了解一下,PowerShell Replace() 方法替换字符串中的文本,示例如下

PowerShell 替换字符串

让我们考虑一下,我们有一个如下所示的字符串

壳极客

我们希望用新字符替换 ShellGeek 字符串中的字符。

要替换的字符 -> Shell

用于替换的新字符 -> PowerShell-

使用PowerShell Replace()方法替换字符串中的字符,我们可以轻松得到预期的结果

"ShellGeel".Replace("Shell","PowerShell-")

将字符 Shell 替换为 PowerShell- 后,上述命令的输出为

PowerShell-Geek

酷提示: PowerShell 字符串连接示例!

PowerShell 替换变量中的字符

让我们考虑这样一个场景,我们有一个字符串存储在变量中,并且想要对字符串中文本的特定部分执行 PowerShell 替换操作。

PS C:\> $strmsg = "ShellGeek"

PS C:\> $strmsg.Replace("Shell","PowerShell-")
PowerShell-Geek

在上面变量中替换字符串的示例中,

  • $strmsg 变量保存“ShellGeek”字符串值。
  • 对变量 $strmsg 使用 Replace() 方法,它找到“Shell”字符串并将其替换为“PowerShell-”文本。
  • 它给出的输出结果为“PowerShell-Geek”

使用 Replace() 在字符串中进行多次替换

要在给定字符串中执行多次替换,我们需要将其链接到一个命令中。

PowerShell Replace() 方法返回修改后的字符串

我们可以链接在一起并调用 Replace() 方法来在字符串中执行多次替换。

命令中的所有方法都是从左到右执行的。

让我们考虑一下下面的示例,我们将计算机名称存储在变量中

PS C:\> $compname = "IT-100,IT-101,IT-102,IT-201,IT-202"

PS C:\> $compname.Replace("IT","Corp").Replace("10","L10").Replace("20","D20")
Corp-L100,Corp-L101,Corp-L102,Corp-D201,Corp-D202

在上面的例子中,

  • $compname 变量包含所有计算机名称的列表
  • $compname.Replace(“IT ”, ”Corp ”) - 将文本“IT”替换为“Corp”
  • $compname.Replace(“IT ”, ”Corp ”).Replace(“10”, ”L10 ”) - 将以 10 开头的计算机名称替换为 L10 (L-笔记本电脑)
  • $compname.Replace(“IT ”, ”Corp ”).Replace(“10”, ”L10 ”).Replace(“20”, ”D20 ”) - 将以 20 开头的计算机名替换为 D20 (D-桌面)
  • 使用从左到右执行的多个 PowerShell Replace() 方法对给定字符串变量进行多次替换

酷提示:您知道如何在 PowerShell 中下载 zip 文件吗?

PowerShell 替换字符串中的字符

使用 Replace() 方法替换字符串中的字符非常简单快捷

PS C:\> $compname = "Corp-L10,Corp-L11,Corp-L12"

PS C:\> $compname.Replace('L','D')
Corp-D10,Corp-D11,Corp-D12

PS C:\> 

在上面的示例中,$compname 有一个计算机名称列表。

任务是找到一个以 L 开头的计算机名称并将其替换为 D。

使用 Replace('L', 'D') 是替换给定字符串中所有出现的特定字符的一种非常快速且简单的方法。

酷提示:您知道如何在 PowerShell 中向字符串或变量添加换行符吗?

使用 PowerShell 替换运算符

语法

-代替

替换运算符 - 不区分大小写。更改左操作数。

PowerShell Replace 运算符 提供了很大的灵活性,因为它使用匹配和替换表达式模式的正则表达式。

让我们通过示例了解 PowerShell 替换运算符,以对字符串执行高级正则表达式替换。

使用 PowerShell 替换字符串单词的运算符

让我们考虑一个使用替换运算符替换字符串中文本的简单示例。

PS C:\> $compname = "IT-100,IT-101,IT-102,IT-201,IT-202"

PS C:\> $compname -replace 'IT','Corp'
Corp-100,Corp-101,Corp-102,Corp-201,Corp-202

PS C:\> 

在上面的例子中,

  • $compname变量存储计算机名称列表
  • 使用 -replace 运算符,找到“IT”并替换为“Corp”
  • $compname -replace ‘IT’,’Corp’ — 将字符 ‘IT’ 替换为文本 ‘Corp’ 并返回修改后的字符串。

PowerShell 使用 -replace 替换字符串中的字符

使用-replace运算符替换给定字符串中的单个字符。

PS C:\> $compname = "Corp-L10,Corp-L11,Corp-L12"

PS C:\> $compname -replace 'L','D'
Corp-D10,Corp-D11,Corp-D12

PS C:\> 

在上面使用替换运算符进行字符替换的示例中

  • $compname 存储计算机名称列表
  • - 替换运算符以查找“L”并替换为字符“D”
  • 返回修改后的字符串

酷提示:你知道如何在 PowerShell 中打印环境变量吗?

使用 PowerShell 替换运算符进行多次替换

要在给定字符串中执行多次替换,我们需要将其链接到一个命令中。

PowerShell 替换运算符在字符串操作后返回修改后的字符串。

我们可以链接在一起并调用替换运算符来在字符串中执行多次替换。

命令中的所有方法都是从左到右执行的。

让我们考虑一下下面的示例,我们将计算机名称存储在变量中

PS C:\> $compname = "IT-100,IT-101,IT-102,IT-201,IT-202"

PS C:\> $compname -replace("IT","Corp") -replace("10","L10") -replace("20","D20")
Corp-L100,Corp-L101,Corp-L102,Corp-D201,Corp-D202

PS C:\> 

在上面的例子中,

  • $compname 变量包含所有计算机名称的列表
  • $compname.Replace(“IT ”, ”Corp ”) - 将文本“IT”替换为“Corp”
  • $compname.Replace(“IT ”, ”Corp ”) -replace(“10”, ”L10 ”) - 将以 10 开头的计算机名称替换为 L10 (L-笔记本电脑)
  • $compname -replace(“IT ”, ”Corp ”) -replace(“10”, ”L10 ”) -replace(“20”, ”D20 ”) —将20开头的计算机名称替换为D20(D-桌面)
  • 使用从左到右执行的多个替换运算符对给定字符串变量进行多次替换

使用 Powershell 正则表达式替换

替换运算符在使用正则表达式 (regex) 来匹配和替换复杂模式方面提供了更大的灵活性。

让我们通过简单的示例来了解使用替换运算符的 PowerShell 正则表达式替换功能。

考虑一个场景,我们有一个计算机名称存储在变量中,并且想要对以“IT”和“CT”开头的计算机名称执行替换操作并替换为“Corp”

PS C:\> $compname = "IT-100,CT-101,IT-102,CT-201,IT-202"

PS C:\> $compname -replace 'IT|CT', 'Corp'
Corp-100,Corp-101,Corp-102,Corp-201,Corp-202

PS C:\> 

在上面的 Powershell 脚本示例中,使用带有替换运算符的正则表达式来替换 |在一个带有新字符串的字符串中。

  • $compname 存储计算机名称列表
  • 使用 -replace 运算符和正则表达式 regex OR (|) 字符,它会在给定字符串中搜索“IT”或“CT”字符
  • 模式匹配后,它将以“IT”或“CT”开头的计算机名称替换为“Corp”

使用 Escape 替换特殊字符(替换正则表达式字符)

在将 PowerShell 正则表达式与 replace 运算符一起使用时,如果正则表达式包含特殊字符或正则表达式字符,则会出现一些陷阱

让我们考虑一个示例,将 [Shell] 替换为 PowerShell,如下所示

PS C:\> "[Shell] Geek" -replace '[Shell]','PowerShell'
[PowerShellPowerShellPowerShellPowerShellPowerShell] GPowerShellPowerShellk

PS C:\> 

在上面的例子中,我们想用PowerShell替换[Shell],上面命令的结果并不符合我们的要求。

由于 [Shell] 在搜索字符串中包含正则表达式特殊字符,因此正确的替换将不起作用。

为了解决上述问题,有两个最佳选择

在搜索字符前面使用反斜杠字符或使用escape()方法

让我们解决上面使用反斜杠字符替换字符串中正则表达式特殊字符的问题

PS C:\> "[Shell] Geek" -replace '\[Shell\]','PowerShell'
PowerShell Geek

PS C:\> 

在上面的示例中,为了避免前面讨论的字符重复问题,因为 PowerShell 正则表达式特殊字符 [ ]。

在这种情况下,使用正则表达式转义字符来转义上例中的方括号。

然而,使用escape()方法的另一种选择是转义所有特殊字符的最佳方法

PS C:\> "[Shell] Geek" -replace ([regex]::Escape('[Shell]')),"PowerShell"
PowerShell Geek

PS C:\> 

酷提示:PowerShell 中的 ErrorAction 和 ErrorVariable 参数!

结论

在上面的博客文章中,我们看到了替换字符串中文本的不同方法,使用 PowerShell Replace() 方法和 -replace 运算符在给定字符串中进行多次替换

使用Replace()方法来简单替换字符串。

但是,如果要执行高级替换操作,请使用-replace运算符和正则表达式。

您可以在 ShellGeek 主页上找到有关 PowerShell Active Directory 命令和 PowerShell 基础知识的更多主题。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯