Replace Words with a linebreak

半腔热情 提交于 2019-12-12 03:48:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!