user6259926 user6259926 - 7 months ago 23
Python Question

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
Jason
Age 54
...


...
...

How do i match - something like
(Name.*(?:(\n)+).*(?:Age))


consider the below -

interface TenGigE0/0/0/7



shutdown

!

interface TenGigE0/0/0/8



bundle id 221 mode active

lacp period short

lacp period short receive 100

lacp period short transmit 100

carrier-delay up 100 down 100

load-interval 30

frequency synchronization

!

transceiver permit pid all

!

interface TenGigE0/0/0/9



mtu 9216

frequency synchronization

!

transceiver permit pid all

!

interface TenGigE0/0/0/10



bundle id 237 mode active

lacp period short

lacp period short receive 100

lacp period short transmit 100

carrier-delay up 120000 down 150

load-interval 30

frequency synchronization


how do i match all the tengigex/x/x/x and corresponding carrier-delay lines.

like below -

[ interface TenGigE0/0/0/8, carrier-delay up 100 down 100]
[ interface TenGigE0/0/0/10, carrier-delay up 120000 down 150]
...and so on.

Answer

The . does not match newlines by default, so we have to use the DOTALL flag. As you clarified, you don#t want the lines in between the matching ones, so we simply put what you want in capturing groups, so that you can easily access them through a tuple later:

Name(.*?)\n(?:.|\n)+?Age(.*?)\n

Check this regex out on regex101.com

You should use it with re.findall(...):

print(re.findall(r"Name(.*?)\n(?:.|\n)+?Age(.*?)\n", data, re.DOTALL)

With your example data, this results int he output:

[(' of person ', ' 42'), (' of person', ' 54')]