2

I have a Powershell script that produces CSV. Only it double quotes double-quotes in a string, whereas every other piece of software I use (in the Unix world anyway) uses \" inside a string instead of "" to mean one double-quote.

Using Powershell regex's, how do I turn this:

"""C:\Java\jre8\bin\java"" -Djava.library.path=""C:\some\path\lib"" -classpath ""some;long;classpath"" com.foo.app.Main"

into this?

"\"C:\Java\jre8\bin\java\" -Djava.library.path=\"C:\some\path\lib\" -classpath \"some;long;classpath\" com.foo.app.Main"

I've tried using the supposed Powershell escape character (backtick), but haven't been able to get the conversion to work.

0

1 Answer 1

1

Use a Regular Expression with a positive lookahead zerolength assertion.

$String='"""C:\Java\jre8\bin\java"" -Djava.library.path=""C:\some\path\lib"" -classpath ""some;long;classpath"" com.foo.app.Main"
$String
$String -replace '"(?="[^"])','\'

yields:

"""C:\Java\jre8\bin\java"" -Djava.library.path=""C:\some\path\lib"" -classpath ""some;long;classpath"" com.foo.app.Main"
"\"C:\Java\jre8\bin\java\" -Djava.library.path=\"C:\some\path\lib\" -classpath \"some;long;classpath\" com.foo.app.Main"
Sign up to request clarification or add additional context in comments.

2 Comments

You answer works, thanks. Unfortunately, it didn't fix my particular problem, because it turns out the string only had single " in it, but it was the | Export-Csv -Path output.csv that was doubling up the ". Not sure if I can fix that without reading in output.csv and transforming that.
Looks like "" for " in CSV is a "standard." I guess I'll just remove the extra " in whatever process reads the CSV.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.