我有一个文件
fill
---example---
fill
fill
---copy---
fill
我想获取
-
之间的文本并像这样写在下面
fill
---example---
example
fill
fill
---copy---
copy
fill
是的,我还想得到我原来的琴弦
我尝试了很多东西,但我得到的最接近的是这个:
$regex = '---(?<text>.*)---'
(Get-Content -Path .\fichier.txt) -replace $regex, "---$($matches['text'])---`n$($matches['text'])" | Set-Content -Path .\fichier.txt
但是文件变成了这样
fill
---example---
example
fill
fill
---example---
example
fill
我怎样才能通过所有正则表达式匹配?
Get-Content的默认行为是: 一次读取一行并返回对象集合,每个对象代表一行内容。
这意味着您可以简单地这样做:
Get-Content .\fichier.txt | ForEach-Object {
$_
if ($_ -Match $regex) { $matches['text'] }
}