0

I am trying a dynamic regex pattern whose occurrence is not known, for instance

ip = '3.3.3.5'
stt ="""
(1.1.1.1/32, 3.3.3.5/32), abcd: xx:xx:xx, abv cd
value: eth1/1 , bbcc , time: tt:tt
text :
eth1/1 ip time << 
eth1/2 ip time <<
 """

what i need to retrieve is , based on the ip i need to get the interface , in the above example , for 3.3.3.5/32 ip , i want to get the interface under "text:" which is eth1/1 and eth1/2

Regex i have used :

re.findall(ip+"[\/0-9,)]+\s+abcd: xx:xx:xx, abv cd\s+value: [0-9a-zA-Z\/]+ , bbcc , time: tt:tt\s+ values text\s+[0-9a-zA-Z\/]+",stt)

output : ['3.3.3.5/32), abcd: xx:xx:xx, abv cd\n value: eth1/1 , bbcc , time: tt:tt\n values text\n  eth1/1']

It returns the first occurrence which is eth1/1, but i am not aware how to get both the interfaces, pls guide

2
  • Why not 1.1.1.1/32 ? Additionally, use the newer regex module which supports \G. Commented May 1, 2017 at 16:42
  • the second ip is mandate , first one is optional , my main requirement is the output under text:. Commented May 1, 2017 at 16:46

2 Answers 2

2

If you just want the output under text: then why not simply:

stt.split('text:', 1)[1].strip()
Sign up to request clarification or add additional context in comments.

3 Comments

i need to traverse the full string , ip should match and the text: is retrieved.
@raj why? What problem does this solution cause you?
what u shared is good , but i need to check a condition like , ip should match first , then the data under text: need to grepped.
1

You could do it as follows:

import regex as re

string = """
(1.1.1.1/32, 3.3.3.5/32), abcd: xx:xx:xx, abv cd
value: eth1/1 , bbcc , time: tt:tt
text :
eth1/1 ip time << 
eth1/2 ip time <<

(1.1.1.1/32, 3.3.4.5/32), abcd: xx:xx:xx, abv cd
value: eth1/1 , bbcc , time: tt:tt
text :
eth1/1 ip time << 
eth1/2 ip time <<

(1.1.1.1/32, 3.3.5.5/32), abcd: xx:xx:xx, abv cd
value: eth1/1 , bbcc , time: tt:tt
text :
eth1/1 ip time << 
eth1/2 ip time <<
"""

rx = re.compile(r'''
        (?:
            \G(?!\A)
            |
            (?P<ip>\d+\.\d+\.\d+\.\d+)/32\)
        )
        (?s:
            (?:(?!^\().)*?
        )
        ^
        (?P<interface>eth\S+)
        \K
    ''', re.VERBOSE|re.MULTILINE)

result = {}; ip = None;
for match in rx.finditer(string):
    if match.group('ip'):
        ip = match.group('ip')
    try:
        result[ip].append(match.group('interface'))
    except:
        result[ip] = [match.group('interface')]

print(result)
# {'3.3.4.5': ['eth1/1', 'eth1/2'], '3.3.3.5': ['eth1/1', 'eth1/2'], '3.3.5.5': ['eth1/1', 'eth1/2']}

This assumes the structure above (IP addresses in parentheses) and uses the second address found.
See a demo on regex101.com.

4 Comments

iam using Python 2.7.3. is regex module available for this ?
Yup, it is, simply pip install regex.
just curious , why regex , why not with re alone ?
@rajpython: \G is not supported in re - try it out, it won't work.

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.