【发布时间】:2020-07-15 14:46:58
【问题描述】:
我有一些匹配我不想包含的行的正则表达式。我曾尝试使用正面和负面的前瞻性,但我不能完全理解它。相反,我想知道是否有一个选项只能匹配最大行数,例如4?
我的部分工作正则表达式是:
TLS 1.1 Cipher suites(?s).*?The server accepted
问题是它包含了前 7 行,我不想包含在内。我只希望它在下面的文本中包含第 11-14 行:
* TLS 1.1 Cipher suites:
Attempted to connect using 80 cipher suites; the server rejected all cipher suites.
* TLS 1.2 Cipher suites:
Attempted to connect using 158 cipher suites.
The server accepted the following 4 cipher suites:
TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 256 ECDH: prime256v1 (256 bits)
* TLS 1.1 Cipher suites:
Attempted to connect using 15 cipher suites.
The server accepted the following 2 cipher suites:
TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA 256 ECDH: prime256v1 (256 bits)
我希望这是有道理的。
【问题讨论】:
-
所以你想得到文件的最后一个匹配项,对吗?
-
不完全是,我只想匹配长度超过 4 行的字符串。在这个例子中,它匹配前 7 行(我不想要)
-
我认为您应该能够使用匹配 4 行的正向前瞻
(?=(?:.*\n){4}) -
您还需要一个 negative 前瞻来防止它包含多个块。
-
神奇!这有效@JvdV。非常感谢!!感谢大家的精彩帮助)))