我想找到文档中与模式列表不匹配的所有非空行。例如,在下面的文档片段中,我想要一个与第2,4,5,6,18,19,20和21行匹配的正则表达式。
我想排除类似于8,10,12,14,16和所有空行的行。
反向模式为(?i)^.*02 December_|^\s*Python Proprietary|^\s*Python Regular Expression Specification|^.*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:
P.S。 -
答案 0 :(得分:3)
您可以使用负面展望:
^(?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).+$
答案 1 :(得分:0)
试试这个
^.*?Python Regular Expression.*?$(*SKIP)(*FAIL)|^.*?Python Proprietary.*?$(*SKIP)(*FAIL)|.*?Page \d+.*?$(*SKIP)(*FAIL)|^$(*SKIP)(*FAIL)|^.*?$
结果:
匹配8行2,4,5,6,18,19,20和21。
说明:
^.*?Python Regular Expression.*?$(*SKIP)(*FAIL)
排除第6,16行。
^.*?Python Proprietary.*?$(*SKIP)(*FAIL)
排除第10行。
.*?Page \d+.*?$(*SKIP)(*FAIL)
排除第12,14行。
^$(*SKIP)(*FAIL)
排除所有空行。
^.*?$
匹配所有其他行。