multi line string replace not working

Welcome Forums General PowerShell Q&A multi line string replace not working

This topic contains 4 replies, has 2 voices, and was last updated by

js
 
Participant
3 months, 2 weeks ago.

  • Author
    Posts
  • #105650

    Participant
    Points: 0
    Rank: Member

    Here is my string where I am trying to replace field 4 on the 1st line with the last field on line 2.
    SBR*P*18**TOWNOFALMOND*****LM~
    NM1*IL*1*KxxxSE*Rxx*M***MI*WRK0012345004~
    If I manually search and replace in TextPad with these regular expressions it works fine:
    (SBR\*P\*\d*)(\*)(\*\S*\nNM1\*\S*)(\*\S*)~
    (\1)(\4)(\3)(\4)~
    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 
  • #105667
    js

    Participant
    Points: 202
    Helping Hand
    Rank: Participant

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

    'SBR*P*18**TOWNOFALMOND*****LM~
    NM1*IL*1*KxxxSE*Rxx*M***MI*WRK0012345004~' -replace '(?ms)(SBR\*\w\*\d{2})(\*)(\*\S*\nNM1\*\S*)(\*\S*)~', 
      '$1$4$3$4~'
    
    # output
    SBR*P*18*WRK0012345004*TOWNOFALMOND*****LM~
    NM1*IL*1*KxxxSE*Rxx*M***MI*WRK0012345004~
    
    # from regex101.com
    Group 1.	0-8	`SBR*P*18`
    Group 2.	8-9	`*`
    Group 3.	9-57	`*TOWNOFALMOND*****LM~
    NM1*IL*1*KxxxSE*Rxx*M***MI`
    Group 4.	57-71	`*WRK0012345004`
  • #105680

    Participant
    Points: 0
    Rank: Member

    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.

    -Kevin

    • #105688
      js

      Participant
      Points: 202
      Helping Hand
      Rank: Participant

      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~
      NM1*IL*1*KxxxSE*Rxx*M***MI*WRK0012345004~'
      
      set-content input $a
      
      emacs input # appears to be unix text
      
    • #105695
      js

      Participant
      Points: 202
      Helping Hand
      Rank: Participant

      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

The topic ‘multi line string replace not working’ is closed to new replies.