Regex - using "replace" to delete content using capture groups

Viewed 40

I have a CSV file containing address information that will occasionally have linebreaks within a field. This results in our software seeing this as two separate lines with invalid data. My final goal is to create a one-line Powershell command that can be run from the terminal to fix this, however, I cannot for the life of me get "-replace" to work correctly when bringing capture groups into the equation.

Here is the data file with line endings that can be captured with "\r\n":

"name1","address1","city1","state1","zip1"
"name2","address2
2ndline2","city2","state2","zip2"

Here is what I have so far (not in the 1 line cmd line format yet):

((get-content "$local\$file" -raw) -replace '(?sm),"[^"]*(?<line>\r\n)[^"]*",', '''${line}<replace>''') `
-replace "<replace>","" | Set-Content "$local\test2.txt"

I ran into issues figuring out how to replace a capture group with nothing, so I am trying to replace it with another string that I can then remove easily. When I run the above command, it replaces the entire match instead of just the capture group. I created this line of code based on another suggestion here: https://stackoverflow.com/a/29973460/6477292

1 Answers

If you have PowerShell Core v6.1 and newer, you may use

(get-content "$local\$file" -raw) -replace ',"[^"\r\n]*\r\n[^"]*",', { $_.Value.Replace("`r`n", "") }

The ,"[^"\r\n]*\r\n[^"]*", regex matches ,", then any 0+ chars other than ", CR and LF, then a CRLF sequence, then any 0+ chars other than " and then a ,.

The { $_.Value.Replace("rn", "") } block takes the match object, and replaces the CRLF sequences with an empty string.

In other versions that do not support the callable block in the replacement of the -replace command, you can only do this with explicitly using the [regex] object:

$regex = [regex]',"[^"\r\n]*\r\n[^"]*",'
$callback = {  param($m) $m.Value.Replace("`r`n", "") }
$newfilecontents = $regex.Replace($filecontents, $callback)
Related