2

I would like to use PowerShell to modify a text file.

I would like to -replace:

  • [REPLACE at the beginning of each line with ENJOY
  • LINE] at the end of each line with LIFE
  • only if the line starts with [REPLACE and ends with LINE]

What would be the regex expression used in -replace to match this request ?

Example - File Input

[REPLACE_IN_THIS_LINE]
[DO_NOT_REPLACE_IN_THIS_LINE_PLEASE]
[REPLACE_VERY_MUCH_IN_THIS_LINE]
Do not replace in this line[REPLACE_IN_THIS_LINE]

Example - File Output

ENJOY_IN_THIS_LIFE
[DO_NOT_REPLACE_IN_THIS_LINE_PLEASE]
ENJOY_VERY_MUCH_IN_THIS_LIFE
Do not replace in this line[REPLACE_IN_THIS_LINE]

As you can see, in this example, only 2nd and 4th lines have changed...

I came up with this for matching the string: -match "^\[REPLACE.*LINE\]$" but I don't know how to use -replace to correctly replace...

  • post regex you're currently using – bestestefan Jul 18 '18 at 08:02
  • @stetoc I just added what I'm using for -match `-match "^\[REPLACE.*LINE\]$"` but I don't know how to transform this to `-replace` to be able to replace only the first and last part and leave the middle as it is... – Jasmin Starvors Jul 18 '18 at 08:08

6 Answers6

1

I think you need to use "^" will validate start of line and "$" will handle end of line. I have written a code in Java but you can use this regex.

String input="[REPLACE_IN_THIS_LINE]"
Pattern Pat=Pattern.compile(("^(\\[REPLACE)(.)*(LINE\\])$"));
Matcher Mat=Pat.matcher();
if(Mat.find())
{
    input=input.replace("[REPLACE","Enjoy").replace("LINE]","Life")
}
Owais Ch
  • 177
  • 1
  • 12
  • If I understood correctly, you break this in 2 steps: 1.check if the string matches the pattern 2. replace what needs to be replaced. – Jasmin Starvors Jul 18 '18 at 08:10
  • Yes I tried to solve this problem by using this solution. – Owais Ch Jul 18 '18 at 09:13
  • Answer needs supporting information Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](https://stackoverflow.com/help/how-to-answer). – moken Aug 05 '23 at 05:35
1

Simply just try string replace functions.

as in Java

String inputValue="[REPLACE_IN_THIS_LINE]".replaceAll("[REPLACE","Enjoy").replaceAll("LINE]","Life");
0

You have to call replace 2 times, one for each string that you want to replace, considering that you use a loop for access each line you must do something like this:

$ResultText = $line.replace("[REPLACE","ENJOY").replace("LINE]","LIFE")

EDIT: I misunderstood the question, I didn't consider that you can have "[REPLACE" in the middle of the text. Try the following:

'[REPLACE_IN_THIS_LINE][REPLACE' -replace '^\[REPLACE(.*?)', 'ENJOY$1'

And then:

'[REPLACE_IN_THIS_LINE][REPLACE_LINE]' -replace '(.+?)LINE\]$', '$1LIFE'

As you can see the first one only replace the first "[REPLACE" and the 2nd one replace only the first "LINE]".

halfer
  • 19,824
  • 17
  • 99
  • 186
Brank Victoria
  • 1,447
  • 10
  • 17
0

try somthing like this :

Get-Content "C:\temp\test.txt" |%{

    if ($_ -like 'ENJOY*LIFE')
    {
      "[REPLACE{0}LINE]" -f $_.Substring(5, $_.Length -9)
    }
    else
    {
        $_
    }


} | Set-Content "c:\temp\result.txt"
Esperento57
  • 16,521
  • 3
  • 39
  • 45
0

Use group placeholders (e.g. $1), see e.g.: Powershell regex group replacing

# $a = Get-Content .\Input.txt
$a = "[REPLACE_IN_THIS_LINE]",
     "[DO_NOT_REPLACE_IN_THIS_LINE_PLEASE]",
     "[REPLACE_VERY_MUCH_IN_THIS_LINE]",
     "Do not replace in this line[REPLACE_IN_THIS_LINE]"

 

$a -Replace '^\[REPLACE(.*)LINE\]$', 'ENJOY$1LIFE'
iRon
  • 20,463
  • 10
  • 53
  • 79
0

They are all very complicated and found this solution for me, to use a regex with the multiline flag to iterate over each line:

powershell -Command "$fileContent = Get-Content -Raw '<your input path>';$fileContent=$fileContent.replace('^\[REPLACE(.*)LINE\\]$', 'ENJOY$1LIFE');Set-Content '<output-path>' -Value $fileContent"
rokdd
  • 541
  • 4
  • 14