条件否定先行

时间:2019-05-22 17:51:18

标签: regex

使用正则表达式匹配字符串([0-99]% loss),即内部数字在099之间。但是,如果文本包含特殊的codeword,则即使%<100,也不应存在匹配项。

例如,这个应该匹配:✓

Packets: Sent = 4, Received = 4, Lost = 0 (0% loss)
Packets: Sent = 100, Received = 1, Lost = 99 (99% loss)

但不是这四个:✗

Test of codeword string:
Packets: Sent = 4, Received = 0, Lost = 4 (100% loss)

Another codeword test:
Packets: Sent = 4, Received = 2, Lost = 2 (50% loss)

Final test:
Packets: Sent = 70, Received = 0, Lost = 70 (100% loss)

Packets: Sent = 4, Received = 4, Lost = 0 (0% loss)
Packets: Sent = 100, Received = 0, Lost = 100 (100% loss)

当前,我已经编写了正则表达式\((?!100)\d+% loss\)来捕获所需的不包含codeword的字符串,但是无法扩展正则表达式以使其阻塞codeword(如果存在) 。我正在考虑某种形式的条件否定超前(例如(?!codeword)|\((?!100)\d+% loss\)?),但是看起来如何?

0 个答案:

没有答案