AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1580562
Accepted
JJM
JJM
Asked: 2020-08-26 06:44:53 +0800 CST2020-08-26 06:44:53 +0800 CST 2020-08-26 06:44:53 +0800 CST

需要正则表达式来查找单词“错误:”,但不匹配“错误:打开本地文件”

  • 772

我正在使用 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)
regex powershell
  • 4 4 个回答
  • 3599 Views

4 个回答

  • Voted
  1. Glorfindel
    2020-08-26T07:04:18+08:002020-08-26T07:04:18+08:00

    正如@spikey_richie 所说,您需要负前瞻:

    ERROR: (?!Opening local file)
    

    基本上,不是指定您要查找的内容,而是指定您不想要的内容,然后将其包装在(?!and)中。

    演示

    • 40
  2. Best Answer
    Lee_Dailey
    2020-08-26T07:15:21+08:002020-08-26T07:15:21+08:00

    这似乎可以满足您的要求 - 它使用 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
    

    输出 ...

    ERROR: Writing local file  (I want to be alerted to this)
    ERROR: Yet another thing
    
    • 14
  3. Hackoo
    2020-08-26T12:32:32+08:002020-08-26T12:32:32+08:00

    类似的东西也应该起作用:Demo

    $MyString = @'
    ERROR: Writing local file  (I want to be alerted to this)
    ERROR: Opening local file  (I don't want to be alerted to this)
    INFO: Some other thing
    ERROR: Yet another thing
    INFO: A thing that is wanted
    WARNING: NOT a not-wanted thing
    '@ -split [System.Environment]::NewLine
    
    $pattern = 'ERROR: (?!Opening local file).+'
    $MyString | %{ [regex]::matches($_,$pattern) } | %{ $_.Groups[0].Value }
    
    • 3
  4. Keith Miller
    2020-08-30T20:00:36+08:002020-08-30T20:00:36+08:00

    这种情况类似于The Greatest Regex Trick Ever中介绍的情况--- 对于任何学习 regex 的人来说都是一本好书。虽然Glorfindel发布的负前瞻在这种情况下效果很好,但这种技术很容易扩展到多个排除、后瞻结合前瞻等,并且仍然保持可读性。该技巧的本质是捕获整个匹配中的所有排除项和第 1 组中的所需内容。对于 OP,正则表达式如下所示:

    'ERROR: Opening local file|(ERROR:.+$)'
    

    示例:(
    在工作代码中,<Here-string> -Split "`n"
    将替换为
    Get-Contnet 'c:\Path\To\Error.log' :)

    @'
    ERROR: Another error
    ERROR: Writing local file  (I want to be alerted to this)
    ERROR: Opening local file  (I don't want to be alerted to this)
    THIS is not an error
    '@ -Split "`n" | ForEach {
        $_ -match 'ERROR: Opening local file|(ERROR:.+$)' | out-null
        $matches[1]
    }
    

    屏幕截图:

    PS C:\> @'
    >> ERROR: Another error
    >> ERROR: Writing local file  (I want to be alerted to this)
    >> ERROR: Opening local file  (I don't want to be alerted to this)
    >> THIS is not an error
    >> '@ -Split "`n" | ForEach {
    >>     $_ -match 'ERROR: Opening local file|(ERROR:.+$)' | out-null
    >>     $matches[1]
    >> }
    ERROR: Another error
    ERROR: Writing local file  (I want to be alerted to this)
    PS C:\>
    

    同样,这种技术的美妙之处在于它易于扩展: 'Exception1|Exception2|Exception3|(Desired Match)'

    • 0

相关问题

  • 将前景颜色添加到 Powershell 配置文件?

  • OneDrive 有 .gitignore 吗?

  • 禁用后无法启用 Microsoft Print to PDF

  • 我可以让这个 PowerShell 脚本接受逗号吗?

  • 如果一个字符串出现在正则表达式中的另一个字符串之前,如何停止搜索

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    v15 为什么通过电缆(同轴电缆)的千兆位/秒 Internet 连接不能像光纤一样提供对称速度? 2020-01-25 08:53:31 +0800 CST
  • Martin Hope
    fixer1234 “HTTPS Everywhere”仍然相关吗? 2019-10-27 18:06:25 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve