我正在[regex]::matches($string, $pattern)
搜索日志文件中两行之间的行。我的文件(节选):
-----end-----
AAA
BBB
CCC
-----start-----
我的代码:
$pattern = "-----end-----(.*?)-----start-----"
$string = Get-Content $input_file
$result = [regex]::matches($string, $pattern)
#Output one of the matches:
Write-Host ($result[0].Value + "`n")
输出:
AAABBBCCC
预期产量:
AAA
BBB
CCC
它以某种方式使沿线的换行符松动,这使得输出(匹配模式之间的许多行)几乎不可读。有什么办法解决吗?
您可以通过使用以下方式解决此问题:
$pattern = "(?s)-----end-----(.*?)-----start-----"
$string = Get-Content $input_file -Raw
$result = [regex]::match($string, $pattern)
Write-Host ($result.Value + "`n")
注意:
(?s)
使.
匹配任何字符,包括换行符Get-Content $input_file -Raw
使用换行符将所有文件内容捕获为单个变量[regex]::match
获取找到的第一个比赛本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句