powershell - 替换字符串中的管道字符

标签 powershell

从SQL查询(Postgres DB)中,以下行输出以字符串形式捕获:

    id         |                           title                            |     cur_status_time  
我正在尝试将管道字符替换为其他字符。
 $line -replace "|",";"
但这不起作用。
我试过了:
$line.replace("|",";")
但它也不起作用。

最佳答案

-replace运算符使用正则表达式匹配。 |是正则表达式中的special character。您需要使用反斜杠\对其进行转义:

$line = $line -replace "\|", ";"
尽管这也应该起作用:
$line = $line.Replace("|", ";")
注意:.NET中的字符串是不可变的(why?)。像Replace这样的操作不会更改原始字符串,因此您必须将结果分配回$line

关于powershell - 替换字符串中的管道字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64577700/

相关文章:

powershell - 在powershell中重命名文件

powershell - 基于文件内容的一线PowerShell批处理重命名

powershell - 为什么 Powershell 将任何字符串与 $null 匹配

c++ - 为什么我的 Windows 桌面进程似乎立即退出而没有标准输出?

powershell - 在PowerShell中将Refresh()发送到WMI

powershell - 如何使用Get-ChildItem在具有特定名称的目录中查找文件?

powershell - Windows Azure Powershell 'you MUST specify a certificate'

xml - 更改XML文件中的元素值

image - 通过Powershell进行智能图像搜索

amazon-web-services - 如何使用 Get-SSMParameterList CmdLet 处理具有特定前缀的参数