0
This post is continuation of the Regular Expression for Network Engineer Part-1 , here we have a look for the different methods to find out the pattern in string.
Findall() – returns list of all the matches the pattern in a string without overlapping
[code language = “Python”]
re.findall(pattern, string[, flags])
In [118]: ip
Out[118]: ‘10.10.1.10,29.10.1.10,10.10.1.20,192.168.1.0,172.16.10.1,10.10.10.121’
In [119]: out= re.findall(r'(10.10.10.\d+)’ ,ip)
In [120]: out
Out[120]: [‘10,10.10.1’, ‘10.10.10.121’]
#Above example help us to find out all the IP’s of subnet 10.10.10.0/24 from group of ip’s.
[/code]
Match()-return a match object when pattern is found at the beginning of string, if no pattern is found ,result in None.
[code language = “Python”]
In [189]: text
Out[189]: ‘Cisco IOS Software, 7200 Software (C7200-SPSERVICESK9-M), Version 12.2(33)SRE, RELEASE SOFTWARE (fc1)’
In [190]: out = re.match(r”Cisco”,text)
In [191]: out
Out[191]: <_sre.SRE_Match object; span=(0, 5), match=’Cisco’>
In [192]: out = re.match(r” Software”,text)
In [193]: out
In [194]: out = re.search(r” Software”,text)
In [195]: out
Out[195]: <_sre.SRE_Match object; span=(9, 18), Continue reading