Skip to content Skip to sidebar Skip to footer

Regex To Match Pattern.one Skip Newlines And Characters Until Pattern.two

I need help with a regex for multi line skip until pattern and couldn't see it already covered. Name of person Jack Nichol Age 42 ..... ..... .... Name of person Andrew

Solution 1:

To match the contents between the closest lines containing tengige and carrier-delay you need a tempered greedy token (or an unrolled version):

(?sim)^([^\n]*TenGigE[^\n]*)(?:(?!TenGigE|carrier-delay).)*([^\n]*carrier-dela‌​y[^\n]*)

See the regex demo

See the Python demo:

import re
p = re.compile(r'^([^\n]*TenGigE[^\n]*)(?:(?!TenGigE|carrier-delay).)*([^\n]*carrier-delay[^\n]*)', re.DOTALL| re.M| re.I)
test_str ="interface TenGigE0/0/0/8\n bundle id 221 mode active\n lacp period short\n lacp period short receive 100\n lacp period short transmit 100\n carrier-delay up 100 down 100\n\ninterface TenGigE0/0/0/7\n\n\n\nshutdown\n\n!\n\ninterface TenGigE0/0/0/8\n\n\n\n bundle id 221 mode active\n\n lacp period short\n\n lacp period short receive 100\n\n lacp period short transmit 100\n\n carrier-delay up 100 down 100\n\n load-interval 30\n\n frequency synchronization\n\n !\n\n transceiver permit pid all\n\n!\n\ninterface TenGigE0/0/0/9\n\n\n\n mtu 9216\n\n frequency synchronization\n\n !\n\n transceiver permit pid all\n\n!\n\ninterface TenGigE0/0/0/10\n\n\n\n bundle id 237 mode active\n\n lacp period short\n\n lacp period short receive 100\n\n lacp period short transmit 100\n\n carrier-delay up 120000 down 150\n\n load-interval 30\n\n frequency synchronization"print(p.findall(test_str))
# => [('interface TenGigE0/0/0/8', 'carrier-delay up 100 down 100'), ('interface TenGigE0/0/0/8', 'carrier-delay up 100 down 100'), ('interface TenGigE0/0/0/10', 'carrier-delay up 120000 down 150')]

UPDATE

A very powerful regex for extracting the same texts based on the unroll the loop technique (unrolled tempered greedy token):

(?sim)^([^\n]*TenGigE[^\n]*\n)[^T\n]*(?:T(?!enGigE)[^T\n]*|\n(?! carrier-delay)[^T\n]*)*(\n carrier-delay[^\n]*)

See the regex demo

Solution 2:

You could come up with:

(?:^(interface\ TenGigE
(?:\d+/?){4}))
(?:(?!(?:carrier-delay|interface))[\s\S])+
(?P<carrier>carrier-delay\ .+)

In Python this would be:

import re
rx = re.compile("""
(?:^(interface\ TenGigE
(?:\d+/?){4}))
(?:(?!(?:carrier-delay|interface))[\s\S])+
(?P<carrier>carrier-delay\ .+)""", re.VERBOSE|re.MULTILINE)
matches = rx.findall(string)

Compared to @Wiktor's answer (which needs > 200k steps), this one only needs ~3k, see a demo on regex101.com (thanks to him for spotting an inaccuracy before).

Post a Comment for "Regex To Match Pattern.one Skip Newlines And Characters Until Pattern.two"