powershellescapingdouble-quotesregexp-replaceedit-in-place

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


I need to remove double quote in a text file, only if lines start with "https". File contents is like this:

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

I've to match "https://example.com", remove both double quotes, leaves the double quotes in other lines, than set-content in place...

I've tried many methods but I'm stuck bacause I don't know how to handle double quotes in a regex, or declaring a filter in "if" or "where" statement and than replace text in place...

Latest try:

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

But not works...

I've read this post and this but I don't understand how to fit these solutions to my needs..

Can Someone help me please?


Solution

  • Read the textfile in as single string using the -Raw switch and then do a regex replace:

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

    If you need to overwrite the text file with the new content, append

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

    to the above.

    Output:

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

    Regex details:

    (             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