如何解决Powershell中的正则表达式可排除以文本开头的所有新行
我有一个类似的文件:
CF1 mytextmytextmytextmytextmytextmytext
CF2 mytextmytextmytextmytextmytextmytextmytextmytextmytextmytextmy
CF3 mytextmytextmytextmytextmytextmytextmytextmytextmytextmytextmytextmytext
CF4 mytextmytextmytextmextmytextmytextmytextmytextmytextmytextmytextmytextmyt
mytextmytextmytextmytextmytextmytextmytextmytextmytextmytextmytextmytext
CF5 mytextmytextmytextmytextmytextmytext
我需要从CF4行中删除新行。
我正在尝试这段代码,但并未删除该换行符:
$content = [System.IO.File]::ReadAllText( "MyFile.txt" )
$content = $content -replace "(`n|`r)?!CF","CF" Write-Host $content
解决方法
您可以使用
(Get-Content $filepath -Raw) -replace "(?>\r\n?|\n)(?!CF)","" > $filepathout
请参见regex demo。详细信息:
-
(?>\r\n?|\n)
-CRLF,LF或CR线结束顺序 -
(?!CF)
-如果当前位置的右侧紧邻有CF
子字符串,则负向超前将使匹配失败。
之前:
之后:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。