python - 如何查找与模式列表不匹配的行?

标签 python regex python-2.7 regex-negation

我想查找文档中与模式列表不匹配的所有非空行。例如,在下面的文档片段中,我想要一个匹配行号 2、4、5、6、18、19、20 和 21 的正则表达式。

我想排除类似于 8、10、12、14、16 的行以及所有空行。

逆模式为 (?i)^.*02 December_|^\s*Python 专有|^\s*Python 正则表达式规范|^.*page\s+\d+|^\s*$ 。我想要一个匹配所有与上述模式不匹配的行的模式。

 1:
 2:This module provides regular expression matching operations.
 3:
 4:Regular expressions use the backslash character ('\') to indicate special forms
 5:or to allow special characters to be used without invoking their special
 6:meaning.
 7:
 8:Python Regular Expression                                           02 December 1999 
 9:
10:                                                                 Python Proprietary 
11:
12:----------------------- Page 292-----------------------
13:
14:PYTHON RE SPECIFICATION Version 2.7 [Vol 9, Part Q]                     page 983 
15:
16:Python Regular Expression Specification 
17:
18:It is important to note that most regular expression operations are available as
19:module-level functions and RegexObject methods. The functions are shortcuts that
20:don’t require you to compile a regex object first, but miss some fine-tuning
21:parameters.
22:

附注-

  1. 我正在使用 re.match()。
  2. 实际文档的每行开头没有行号。为了便于讨论,已在此代码段中添加了行号。

最佳答案

您可以使用否定前瞻:

正则表达式

^(?i)(?!-+\s+Page\s+\d+-+|Python\s+Regular\s+Expression\s+\d{2}.+\d{4}|.+Python\s+Proprietary|PYTHON\s+RE SPECIFICATION\s+Version.+\s+page\s+\d+|Python\s+Regular\s+Expression\s+Specification).+$

演示

Click to view

描述

Regular expression visualization

关于python - 如何查找与模式列表不匹配的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35982458/

相关文章:

python - 如何使用azure DevOps python API的文档,我试图在进行API调用时获取对象拥有哪些成员?

python - Pygame 鼠标移动失败

python - 从 python 中定期运行 Scrapy 会导致 ReactorAlreadyRunning

regex - grep 前 n 行,只返回文件名

python - 有没有办法显示正确的浮点值?

python - Visual Studio 代码 pylint : Unable to import 'protorpc'

python - 在 kivy 中让标签淡出

php - 总是返回 false 的正则表达式

mysql - 正则表达式能解决这个问题吗?我们如何获取表达式中使用的参数数组?

python-2.7 - 用于频率计数的意大利句子词形化