powershell – copy a part of a regex under the line where it has been found

  Kiến thức lập trình

I got a file

fill
---example---
fill
fill
---copy---
fill

and I want to get the text between - and write it below like this

fill
---example---
example
fill
fill
---copy---
copy
fill

Yes, I still want to get my original strings

I tried a lot of things but the closest one I got is this :

$regex = '---(?<text>.*)---'
(Get-Content -Path .fichier.txt) -replace $regex, "---$($matches['text'])---`n$($matches['text'])" | Set-Content -Path .fichier.txt

but the file becomes this

fill
---example--- 
example
fill
fill
---example--- 
example
fill

How can I get thru all regex matches ?

LEAVE A COMMENT