首页 > 解决方案 > 只返回不包含特定单词的行

问题描述

我有这段文字

2/27/2020 7:00:43 PM  138 (6036)   Downloading view page: /personal/test/Documents/Forms/All.aspx...
2/27/2020 7:00:43 PM  138 (6036)    Downloading page WebParts...
2/27/2020 7:00:44 PM  138 (6036)     Downloading page web parts completed. 1 web parts successfully downloaded.
2/27/2020 7:00:44 PM  138 (6036)     Downloading page content...
2/27/2020 7:00:44 PM  138 (6036)       Restarting from position 8312...
2/27/2020 7:00:44 PM  138 (6036) Error:  error
double double
2/27/2020 7:00:43 PM  138 (6036)   Downloading view page: /personal/test/sdadasda/Forms/All.aspx...
2/27/2020 7:00:44 PM  138 (6036) Error: Unspecified error
2/27/2020 7:00:43 PM  138 (6036)   Downloading view page: /personal/test/21312332131/Forms/All.aspx...
2/27/2020 7:00:43 PM  138 (6036)   Downloading view page: /personal/test/123/Forms/All.aspx...
2/27/2020 7:00:43 PM  138 (6036)   Downloading view page: /personal/test/123/Forms/All.aspx...
2/27/2020 7:00:43 PM  138 (6036)   Downloading view page: /personal/test/Documeents/Forms/All.aspx...
2/27/2020 7:00:43 PM  138 (6036)   Downloading view page: /personal/test/wr/Forms/All.aspx...

我需要返回所有行:

预期结果:

2/27/2020 7:00:43 PM  138 (6036)   Downloading view page: /personal/test/21312332131/Forms/All.aspx...
2/27/2020 7:00:43 PM  138 (6036)   Downloading view page: /personal/test/123/Forms/All.aspx...
2/27/2020 7:00:43 PM  138 (6036)   Downloading view page: /personal/test/123/Forms/All.aspx...
2/27/2020 7:00:43 PM  138 (6036)   Downloading view page: /personal/test/Documeents/Forms/All.aspx...
2/27/2020 7:00:43 PM  138 (6036)   Downloading view page: /personal/test/wr/Forms/All.aspx...

我尝试了负前瞻和常规 [^Docuemnts]。似乎没有一个工作。

(\(\d+\)).*Downloading view page:\s\/personal\/\S+\/[^(Documents)]\/F
(\(\d+\)).*Downloading view page:\s\/personal\/\S+\/(?!Documents)\/F

标签: regexregex-lookarounds

解决方案


一个简单的解决方案是只使用一个否定的前瞻来排除Documents

^(?!.*\bDocuments\b).*\bDownloading view page:(?!\S).*$

演示

以下是正则表达式模式的完整解释:

^                           from the start of the string
(?!.*\bDocuments\b)         assert that "Documents" does not occur anywhere in the line
.*\bDownloading view page:  then match "Downloading view page:"
(?!\S)                      assert that what follows is either whitespace or end of string
.*                          match the rest of the line
$                           until the end of the string

推荐阅读