问题
This is my Textfile (temp.txt):
(test1 AND id=42343) AND NOT (test2 AND id=12234) AND NOT (test3 AND id=12342) AND NOT (test4 AND id=13342)
I have to replace all ")AND NOT(" so that the file after formatation look like this:
test1 AND id=42343
test2 AND id=12234
test3 AND id=12342
test4 AND id=13342
I tried this Script/Command:
(Get-Content C:\temp.txt) |ForEach-Object{$_ -replace "[) AND NOT (]","`n`r"}|Set-Content C:\temp.txt
But now the textfile looks terrible:
es
1
i
=42343
es
2
i
=12234
es
3
i
=12342
es
4
i
=13342
So what is wrong with my Command?
回答1:
Try:
$a = "(test1 AND id=42343) AND NOT (test2 AND id=12234) AND NOT (test3 AND id=12342) AND NOT (test4 AND id=13342)"
$a.Replace(") AND NOT (", "`r`n").TrimStart("(").TrimEnd(")") | Out-File E:\2.txt
You don't really need regex here, or even a loop
来源:https://stackoverflow.com/questions/36763013/replace-words-with-a-linebreak