I am kindof stuck in extracting an IPv4 address from a String.
My input string & constraints are as follows :
0.0.0.0
to 255.255.255.255
this is an ip
& this is an ip 200.100.2.32
200.100.2.32 is an ip
| Output : ['200.100.2.32']
200.100.2.32is an ip
| Output : []
the ip is 200.100.2.32
| Output : ['200.100.2.32']
the ip is200.100.2.32
| Output : []
the ip is 200.100.2.32 and it is ipv4
| Output : ['200.100.2.32']
the ip is 200.100.2.32and it is ipv4
| Output : []
200.100.2.32 100.50.1.16
| Output : ['200.100.2.32', '100.50.1.16']
200.100.2.32.100.50.1.16
| Output : []
I am trying to build a regex for the above cases, they look fairly straightforward, and I am not able to incorporate all the regex checks.
I have been referring to the answers on these links : Link1, Link2, Link3
Can someone help me in the right direction? To Summarize :
0.0.0.0
to 255.255.255.255
Code
def find_ip(str) :
ip_pattern = re.compile('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\s') # need to strengthen the regex here
ip = re.findall(ip_pattern, str)
return ip
The regex:
(?:^|\b(?<!\.))(?:1?\d\d?|2[0-4]\d|25[0-5])(?:\.(?:1?\d\d?|2[0-4]\d|25[0-5])){3}(?=$|[^\w.])
An example of matches.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With