-1

I'm trying to replace a quotation mark with two quotation marks (escaping the quotation mark) within a csv file unless it's being used as a text qualifier in Powershell. I'm doing this by replacing the quotation mark unless it's preceded by a comma OR succeeded by a carriage return or comma. I was able to do this by using:

Get-Content $input1 -Raw | ForEach { $_ -replace '(?<!,)"(?![,\r\f\n])', '""' })

That worked great, but I didn't think about the situation where the first character of a line could possibly be a quotation mark. I tried to use the following:

Get-Content $input1 -Raw | ForEach { $_ -replace '(?!^)(?<!,)"(?![,\r\f\n])', '""' })

The regex matches correctly when I run a test on https://regex101.com/, but when I run the Powershell script, it replaces the first quotation mark with two. Here is a sample of data I'm using:

"JJOO1","4" top","this is a test","07/15/2022",""

It should only be matching on the quotation mark after 4, but for some reason Powershell is matching on the first quotation mark before JJOO1 and the quotation mark after 4.

InSync
  • 4,851
  • 4
  • 8
  • 30
Jacob
  • 151
  • 1
  • 8

0 Answers0