none
Powershell regex replace() won't replace text RRS feed

  • Question

  • Consider the following multi-line data:

    ST*876*0001  
    G92*RZ*20190507*040245444  
    N9*2I*00005  
    G61*BD*Doe, John  
    G62*02*20190510  
    G62*10*20190508  
    NTE*GEN*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX  
    NTE*GEN*DAN CELL: 555.555.1234  
    NTE*GEN*555-555-1212  

    I'm trying to use a regex with the replace function to remove the colon character in the second NTE line, but keep whatever character immediately precedes it.

    If I used a straight text replace 'L:', 'L', the colon is removed.  However, making the replace call more generic fails to remove the colon, even though the regex search is successful.  Powershell version 5.

    Escaping the colon in the regex doesn't work, and not escaping the colon character also doesn't work.

        $files = Get-ChildItem "\\path\to\bunch\of\files\*.dat"
        $regAWGNote2 = '^NTE\SGEN\S.+:'
        
        ForEach ($file in $files)
        {
            if (Select-String $regAWGNote2 $file)
            {
                echo "Found it (file - $file)"
                echo "removing the colon character..."
                $fileContent = Get-Content($file)
                $fileContent = $fileContent.replace('([A-Z]):', '$1')
                $fileContent | out-file -Encoding ASCII $file
            }
        }

    I expect the colon to be removed, and replaced with nothing, but only if there is a capital letter immediately preceding the colon.  Any other colon found in the file, if it's not preceded by a capital letter, should be ignored.

    The colon is never removed from the resulting file, so I'm doing something wrong in my regex replace() parameter.

    This regex finds the colon: '^NTE\SGEN\S.+:'
    This regex doesn't remove the colon in a replace() call:
    '([A-Z]):'
    '([A-Z])\:'

    As always, greatly appreciate any insight you can provide.
    Friday, May 10, 2019 2:25 PM

Answers

  • $fileContent = $fileContent -replace '([A-Z]):', '$1'

    https://stackoverflow.com/questions/10184156/whats-the-difference-between-replace-and-replace-in-powershell


    While @Keith Hill's answer explains the difference between Replace method and the -replace operator, to explain why you might not see the same result, it is because you are using the String.Replace method which does string replace and -replace operator uses regex replace. You can use the Regex.Replace method for this purpose and you should see the same effect:

    • Marked as answer by Baloosh Friday, May 10, 2019 2:53 PM
    Friday, May 10, 2019 2:47 PM

All replies

  • $fileContent = $fileContent -replace '([A-Z]):', '$1'

    https://stackoverflow.com/questions/10184156/whats-the-difference-between-replace-and-replace-in-powershell


    While @Keith Hill's answer explains the difference between Replace method and the -replace operator, to explain why you might not see the same result, it is because you are using the String.Replace method which does string replace and -replace operator uses regex replace. You can use the Regex.Replace method for this purpose and you should see the same effect:

    • Marked as answer by Baloosh Friday, May 10, 2019 2:53 PM
    Friday, May 10, 2019 2:47 PM
  • Holy Moly - you are the man.  That was it.  Using -Replace instead allows the removal of the colon character.

    Thank you!

    Friday, May 10, 2019 2:53 PM