替换文本字段中不必要的双引号 [英] replace the unnecessary double quotation marks in the text field

查看:0
本文介绍了替换文本字段中不必要的双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试替换名称中不必要的双引号,而不是双引号。目前,我知道这些名称,但我希望代码是动态的。我们非常感谢您的任何建议。

$FilePath = "C:Test"
Get-ChildItem $FilePath -Filter | ForEach-Object {
(Get-Content $_.FullName -Raw) | Foreach-Object {
$_ -replace  ',"Frederick "Fred"",' , ',"Frederick Fred",' `
-replace  ',"Brian "Josiah"",' , ',"Brian Josiah",' ` 
-replace  ',""",' , ',"",'
} | Set-Content $_.FullName
}

推荐答案

使用regex

# Sample input
$str = '"Frederick "Fred"","Brian "Josiah"",""""'

$str -replace '(?m)(?<=,|^)"([^,"]*)"([^,"]*)""(?=,|$)', '"$1$2"'

输出:

"Frederick Fred","Brian Josiah",""
有关正则表达式及其实验机会的说明,请参阅this regex101.com page(请注意"字符。在链接的页面上被转义为",因为正则表达式作为一个整体被括在"..."中,与'...'不同,如上所述)。

这篇关于替换文本字段中不必要的双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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