Powershell就地删除文本文件中的双引号,其中行以双引号+一些其他文本开头

powershell in-place remove double quotes in text file where line starts with double quotes + some other text

我需要删除文本文件中的双引号,前提是行以 "https" 开头。文件内容是这样的:

...
    "bla, bla, bla"
    "https://example.com"
    "bar, bar, bar"
...

我必须匹配“https://example.com”,删除两个双引号,将双引号留在其他行中,而不是设置内容...

我尝试了很多方法,但我被卡住了,因为我不知道如何处理正则表达式中的双引号,或者在 "if" 或 "where" 语句中声明过滤器,然后就地替换文本...

最近尝试:

$TextFile = Get-Content "e:\file.txt"
foreach ($Line in $TextFile) {if ($Line.StartsWith('"https')) { $line.trim('"')} | Set-Content $TextFile

但是不行...

我读过 post and this 但我不明白如何让这些解决方案满足我的需求..

有人可以帮帮我吗?

获取不带双引号的 'https://' 字符串:

$content = Get-Content PATH TO YOUR FILE

foreach( $line in $content ) {
    if( $line -match "(`"https:\/\/)" ) {
        $line -replace '"',''
    }
}

使用 -Raw 开关以单个字符串形式读取文本文件,然后执行正则表达式替换:

(Get-Content "e:\file.txt" -Raw) -replace '(\s*)"(https:[^"]+)"',''

如果需要用新内容覆盖文本文件,请追加

| Set-Content -Path "e:\file.txt" -Force

以上。

输出:

...
    "bla, bla, bla"
    https://example.com
    "bar, bar, bar"
...

正则表达式详细信息:

(             Match the regular expression below and capture its match into backreference number 1
   \s         Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
      *       Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
)
"             Match the character “"” literally
(             Match the regular expression below and capture its match into backreference number 2
   https:     Match the characters “https:” literally
   [^"]       Match any character that is NOT a “"”
      +       Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
"             Match the character “"” literally