0

My output is as below

0.0.0.0/0
  unicast-ip4-chain
  [@0]: dpo-load-balance: [proto:ip4 index:86 buckets:1 uRPF:102 to:[0:0]]
    [0] [@0]: dpo-drop ip4
0.0.0.0/32
  unicast-ip4-chain
  [@0]: dpo-load-balance: [proto:ip4 index:87 buckets:1 uRPF:88 to:[0:0]]
    [0] [@0]: dpo-drop ip4
1.1.1.254/32
  unicast-ip4-chain
  [@0]: dpo-load-balance: [proto:ip4 index:72 buckets:1 uRPF:41 to:[0:0]]
    [0] [@5]: ipv4 via 2.2.2.254 VirtualFuncEthernet0/7/0.1540: mtu:1500 f8c001181ac0fa163e81a6c0810006040800
14.1.1.0/32
  unicast-ip4-chain
  [@0]: dpo-load-balance: [proto:ip4 index:14 buckets:1 uRPF:111 to:[0:0]]
    [0] [@0]: dpo-drop ip4
14.1.1.1/32
  unicast-ip4-chain
  [@0]: dpo-load-balance: [proto:ip4 index:54 buckets:1 uRPF:61 to:[1228:75011]]
    [0] [@5]: ipv4 via 14.1.1.1 VirtualFuncEthernet0/7/0.1540: mtu:1500 f8c001181ac0fa163e81a6c0810006040800

To capture the value 2.2.2.254 from the output I have written regexp as below.

var = 1.1.1.254/32
re.findall(var+r'.*ipv4\s+via\s+(\W+)', x1)

Current output is []

1
  • Unless you are specifically asking about how to solve a cross-version compatibility problem (in which case your question should obviously describe that problem) you should not mix the python-2.7 and python-3.x tags. I have edited to remove the former. Commented Jul 23, 2020 at 11:34

2 Answers 2

2

You may use

(?ms)^1\.1\.1\.254/32\n.*?ipv4\s+via\s+(\d[\d.]*)

See the regex demo

Details

  • (?ms) - re.M and re.DOTALL enabled
  • ^ - start of a line
  • 1\.1\.1\.254/32 - 1.1.1.254/32 string
  • \n - a newline
  • .*? - any 0 or more chars as few as possible
  • ipv4\s+via\s+ - ipv4, 1+ whitespaces, via, 1+ whitespaces
  • (\d[\d.]*) - Capturing group 1: a digit and then 0 or more digits / dots

Python demo:

import re
text = "0.0.0.0/0\n  unicast-ip4-chain\n  [@0]: dpo-load-balance: [proto:ip4 index:86 buckets:1 uRPF:102 to:[0:0]]\n    [0] [@0]: dpo-drop ip4\n0.0.0.0/32\n  unicast-ip4-chain\n  [@0]: dpo-load-balance: [proto:ip4 index:87 buckets:1 uRPF:88 to:[0:0]]\n    [0] [@0]: dpo-drop ip4\n1.1.1.254/32\n  unicast-ip4-chain\n  [@0]: dpo-load-balance: [proto:ip4 index:72 buckets:1 uRPF:41 to:[0:0]]\n    [0] [@5]: ipv4 via 2.2.2.254 VirtualFuncEthernet0/7/0.1540: mtu:1500 f8c001181ac0fa163e81a6c0810006040800\n14.1.1.0/32\n  unicast-ip4-chain\n  [@0]: dpo-load-balance: [proto:ip4 index:14 buckets:1 uRPF:111 to:[0:0]]\n    [0] [@0]: dpo-drop ip4\n14.1.1.1/32\n  unicast-ip4-chain\n  [@0]: dpo-load-balance: [proto:ip4 index:54 buckets:1 uRPF:61 to:[1228:75011]]\n    [0] [@5]: ipv4 via 14.1.1.1 VirtualFuncEthernet0/7/0.1540: mtu:1500 f8c001181ac0fa163e81a6c0810006040800"
v = "1.1.1.254/32"
m = re.search(rf"^{re.escape(v)}\n.*?ipv4\s+via\s+(\d[\d.]*)", text, re.M|re.DOTALL)
if m:
    print(m.group(1))
# => 2.2.2.254
Sign up to request clarification or add additional context in comments.

2 Comments

how can we capture the multiple IP, if I have to capture multiple IP. eg : 1.1.1.254/32 unicast-ip4-chain [@0]: dpo-load-balance: [proto:ip4 index:214 buckets:4 uRPF:230 to:[0:0]] [0] [@5]: ipv4 via 2.2.2.254 Interface0/6/0.2000: mtu:1500 f8c001189ac0fa163e77d390810005e40800 [1] [@5]: ipv4 via 3.3.3.254 Interface0/6/0.2001: mtu:1500 f8c001189ac0fa163e77d390810005e50800 [2] [@5]: ipv4 via 4.4.4.254 Interface0/6/0.2002: mtu:1500 f8c001189ac0fa163e77d390810006300800 [3] [@5]: ipv4 via 5.5.5.254 Interface0/6/0.2003: mtu:1500 f8c001189ac0fa163e77d390810006310800
Written a regexp as below as below for the same, to capture 2.2.2.54, 3.3.3.254, 4.4.4.254, 5.5.5.254 Python Code for the same
0

If you want the whole block of text until next IP try this: https://regex101.com/r/wCHYvj/2

(1\.1\.1\.254\/32)(\s*|.*)+?(?=\d{1,3}.\d{1,3}.\d{1,3})

find given IP and capture until next IP in new line

2 Comments

(\s*|.*)+? will lead to huge performance issues and catastrophic backtracking with non-matching inputs.
@WiktorStribiżew good point, thx. I'll edit my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.