我正在用 PowerShell 编写一个错误检查脚本来查看 Bitvise SFTP 日志并在出现任何错误时发出警报。但是,我不希望它在字符串“错误:打开本地文件”上提醒我,因为这只是告诉我该文件已存在于目标中,这是预期的。我不太擅长正则表达式,除了我们可以忽略的错误之外,我似乎无法找到正确的元素组合来查找错误。
例子:
ERROR: Writing local file (I want to be alerted to this)
ERROR: Opening local file (I don't want to be alerted to this)
Run Code Online (Sandbox Code Playgroud)
Glo*_*del 40
正如@spikey_richie 所说,你需要负前瞻:
ERROR: (?!Opening local file)
Run Code Online (Sandbox Code Playgroud)
基本上,不是指定您要查找的内容,而是指定您不想要的内容,并将其包装在(?!和 中)。
小智 14
这似乎可以满足您的需求 - 它使用 PoSh 可以将正则表达式匹配应用于整个集合的方式。[咧嘴笑]
它能做什么 ...
#region/#endregion块Get-Content。$Result集合编码 ...
#region >>> fake reading in a text file
# in real life, use Get-Content
$InStuff = @'
ERROR: Writing local file (I want to be alerted to this)
ERROR: Opening local file (I do not want to be alerted to this)
INFO: Some other thing
ERROR: Yet another thing
INFO: A thing that is wanted
WARNING: Something that may be wanted
'@ -split [System.Environment]::NewLine
#endregion >>> fake reading in a text file
$TargetPattern = 'error:'
$NotWantedPattern = ': opening local file'
$Result = $InStuff -notmatch $NotWantedPattern -match $TargetPattern
$Result
Run Code Online (Sandbox Code Playgroud)
输出 ...
ERROR: Writing local file (I want to be alerted to this)
ERROR: Yet another thing
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3255 次 |
| 最近记录: |