在 PowerShell 中使用 Trim 或 Replace 清除引号中未包含的任何内容

Using either Trim or Replace in PowerShell to clean up anything not contained in quotation marks

我正在尝试编写一个脚本,该脚本将从 SQL 查询生成的结果集中删除引号中包含的文本以外的所有内容。不确定 trim 或 -replace 是否会这样做。

这是结果集的示例:

a:5:{s:3:"Client Service";a:4:{s:15:"Client Training";b:0;s:11:"Payer Error";

我希望它最终看起来像这样:

Client Service
Client Training
Payer Error

我已经尝试了我对 PowerShell 和 RegEx 的有限了解所做的一切,但仍然无法找到一个好的解决方案。

如有任何帮助,我们将不胜感激。

$s = 'a:5:{s:3:"Client Service";a:4:{s:15:"Client Training";b:0;s:11:"Payer Error";'

替换字符串开头到第一个引号,或最后一个引号到字符串结尾。那么你剩下的是:

   Client Service";a:4:{s:15:"Client Training";b:0;s:11:"Payer Error

现在您不需要的位是 "in quotation marks",这很容易与 ".*?" 匹配,因此将其替换为 space。

总的来说,两个替换:

$s -replace '^[^"]*"|"[^"]*$' -replace '".*?"', ' '

Client Service Client Training Payer Error

这是一个版本,它使用正则表达式将包括引号在内的字符串捕获到数组中,然后使用 -replace:

删除引号
$text = 'a:5:{s:3:"Client Service";a:4:{s:15:"Client Training";b:0;s:11:"Payer Error";'

([regex]::matches($Text, '(")(.*?)(")')).value -replace '"'

毫无疑问,正则表达式首先获取不带引号的字符串,但我是一个正则表达式新手。