我想查找文档中与模式列表不匹配的所有非空行。例如,在下面的文档片段中,我想要一个匹配行号 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:
附注-
- 我正在使用 re.match()。
- 实际文档的每行开头没有行号。为了便于讨论,已在此代码段中添加了行号。
最佳答案
您可以使用否定前瞻:
正则表达式
^(?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).+$
演示
描述
关于python - 如何查找与模式列表不匹配的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35982458/