Author Posts

July 31, 2018 at 2:59 pm

Here is my string where I am trying to replace field 4 on the 1st line with the last field on line 2.
If I manually search and replace in TextPad with these regular expressions it works fine:
This code never finds a match...

[IO.Directory]::SetCurrentDirectory((Convert-Path (Get-Location -PSProvider FileSystem)))
 $filetxt = [IO.File]::ReadAllText("C:\test2\QTR2WHCfull\QTR2WHC\test\81xxx1918WIMedicaidHosp1_837_5.DAT") 

 $filetxt = ($filetxt -match "(?ms)(SBR\*\w\*\d{2})(\*)(\*\S*\nNM1\*\S*)(\*\S*)~", "(\1)(\4)(\3)(\4)~")

 $filetxt #this outputs False which I assume it is NOT finding the expression. 

 I haven't gotten to this line because it is not finding a match above. 

 Set-Content -Path "C:\test2\QTR2WHCfull\QTR2WHC\test\81xxx1918WIMedicaidHosp1_837_5.DAT" -Value $filetxt 

July 31, 2018 at 4:08 pm

Use $1 $4 $3 $4 and single quotes.

NM1*IL*1*KxxxSE*Rxx*M***MI*WRK0012345004~' -replace '(?ms)(SBR\*\w\*\d{2})(\*)(\*\S*\nNM1\*\S*)(\*\S*)~', 

# output

# from
Group 1.	0-8	`SBR*P*18`
Group 2.	8-9	`*`
Group 3.	9-57	`*TOWNOFALMOND*****LM~
Group 4.	57-71	`*WRK0012345004`

July 31, 2018 at 6:49 pm

That worked! How do I get that to do a replace for all files in a folder. You can see my attempt above. I attempted my code above but it didn't error or update.


July 31, 2018 at 7:37 pm

For some reason the input file has to be unix text (linefeeds only), not windows text (linefeeds and carriage returns). "get-content -raw" should work as well.

$a = 'SBR*P*18**TOWNOFALMOND*****LM~

set-content input $a

emacs input # appears to be unix text

July 31, 2018 at 8:38 pm

Replacing \n with .*? will fix it. Just .* will match too much. \n won't match a windows carriage return. The s in (?ms) lets .* include line breaks. \r\n works too for windows text, or \s\s, or .. In fact you don't need the (?ms) if you use \s\s or \r\n. Or just use (?s) with .*? I'm not sure what the m does.

$a = get-content -raw input
$a = $a -replace '(?ms)(SBR\*\w\*\d{2})(\*)(\*\S*.*?NM1\*\S*)(\*\S*)~','$1$4$3$4~'
set-content input $a