My text file "reg1.txt" goes like:
Python trainings going on well We are connecting to server having IP 192.168.101.124 for Python hands-on My email id is john1@xyz.com use this email for official purpose. Python server IP is 101.201.17.155 used at Cityone campus PYThon server IP is 101.201.101.5 used at Citytwo campus My friend email id is peter1@xyz.com use this email for official purpose. My manager email id is cooldude@xyz.com use this email for official purpose. The PYTHON server IP is 173.101.255.15 used at Citythree campus The Testing server IP is 95.101.175.101 used at Citythree campus
The problem is to find all the IPs in the file. My code goes like:
import re
import os
f1=open("reg1.txt","r")
for line in f1:
rx=re.search("(\d{1,3}.){3}\d{1,3}",line)
print(rx)
f1.close()
f2=open("reg1.txt","r")
for line in f2:
rx=re.search("(\d{1,3}.){3}\d{1,3}",line)
if rx:
print(rx.groups())
f2.close()
My console shows results:
<re.Match object; span=(38, 53), match='192.168.101.124'>
None
<re.Match object; span=(34, 48), match='101.201.17.155'>
<re.Match object; span=(20, 33), match='101.201.101.5'>
None
None
<re.Match object; span=(24, 38), match='173.101.255.15'>
<re.Match object; span=(25, 39), match='95.101.175.101'>
('101.',)
('17.',)
('101.',)
('255.',)
('175.',)
Why the code prints only the 3rd portion of the matched object when the matching shows the full span of the ip address?
How to print the whole IP address ?