Open In App

Python program to find the type of IP Address using Regex

Last Updated : 02 Sep, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: Python Regex

Given an IP address as input, write a Python program to find the type of IP address i.e. either IPv4 or IPv6. If the given is neither of them then print neither.

What is an IP (Internet Protocol) Address?

Every computer connected to the Internet is identified by a unique four-part string, known as its Internet Protocol (IP) address. IPv4 and IPv6 are internet protocol version 4 and internet protocol version 6, IP version 6 is the new version of Internet Protocol, which is way better than IP version 4 in terms of complexity and efficiency.

  • IPv4 was the primary version brought into action for production within the ARPANET in 1983. IP version four addresses are 32-bit integers which will be expressed in hexadecimal notation.
  • IPv6 was developed by the Internet Engineering Task Force (IETF) to deal with the problem of IP v4 exhaustion. IP v6 is 128-bits address having an address space of 2128, which is way bigger than IPv4. In IPv6 we use Colon-Hexa representation. There are 8 groups and each group represents 2 Bytes.

Examples:

Input: 192.0.2.126
Output: IPv4

Input: 3001:0da8:75a3:0000:0000:8a2e:0370:7334
Output: IPv6

Input: 36.12.08.20.52
Output: Neither

Approach:

  • Take the IP address as input.
  • Now, check if this IP address resembles IPv4 type addresses using regex.
  • If yes, then print “IPv4” else check if this IP address resembles IPv6 type addresses using regex.
  • If yes, then print “IPv6”.
  • If the address doesn’t resemble any of the above types then we will print “Neither”.

Below is the implementation of the above approach:

Python3




# Python program to find the type of Ip address
 
# re module provides support
# for regular expressions
import re
 
# Make a regular expression
# for validating an Ipv4
ipv4 = '''^(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(
            25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(
            25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(
            25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)$'''
 
# Make a regular expression
# for validating an Ipv6
ipv6 = '''(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|
        ([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:)
        {1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1
        ,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}
        :){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{
        1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA
        -F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a
        -fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0
        -9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,
        4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}
        :){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9
        ])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0
        -9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]
        |1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]
        |1{0,1}[0-9]){0,1}[0-9]))'''
 
# Define a function for finding
# the type of Ip address
def find(Ip): 
   
    # pass the regular expression
    # and the string in search() method
    if re.search(ipv4, Ip):
        print("IPv4")
    elif re.search(ipv6, Ip):
        print("IPv6")
    else:
        print("Neither")
 
# Driver Code 
if __name__ == '__main__'
       
    # Enter the Ip address
    Ip = "192.0.2.126"
       
    # calling run function 
    find(Ip)
   
    Ip = "3001:0da8:75a3:0000:0000:8a2e:0370:7334"
    find(Ip)
   
    Ip = "36.12.08.20.52"
    find(Ip)


Output:

IPv4
IPv6
Neither


Previous Article
Next Article

Similar Reads

Validate an IP address using Python without using RegEx
Given an IP address as input, the task is to write a Python program to check whether the given IP Address is Valid or not without using RegEx. What is an IP (Internet Protocol) Address? Every computer connected to the Internet is identified by a unique four-part string, known as its Internet Protocol (IP) address. An IP address (version 4) consists
2 min read
How to validate an IP address using ReGex
Given an IP address, the task is to validate this IP address with the help of Regex (Regular Expression) in C++ as a valid IPv4 address or IPv6 address. If the IP address is not valid then print an invalid IP address. Examples: Input: str = "203.120.223.13" Output: Valid IPv4 Input: str = "000.12.234.23.23" Output: Invalid IP Input: str = "2F33:12a
5 min read
Python program to find files having a particular extension using RegEx
Prerequisite: Regular Expression in Python Many of the times we need to search for a particular type of file from a list of different types of files. And we can do so with only a few lines of code using python. And the cool part is we don't need to install any external package, python has a built-in package called re, with which we can easily write
2 min read
Python | Program that matches a word containing 'g' followed by one or more e's using regex
Prerequisites : Regular Expressions | Set 1, Set 2 Given a string, the task is to check if that string contains any g followed by one or more e's in it, otherwise, print No match. Examples : Input : geeks for geeks Output : geeks geeks Input : graphic era Output : No match Approach : Firstly, make a regular expression (regex) object that matches a
2 min read
Python program to Count Uppercase, Lowercase, special character and numeric values using Regex
Prerequisites: Regular Expression in Python Given a string. The task is to count the number of Uppercase, Lowercase, special character and numeric values present in the string using Regular expression in Python. Examples: Input : "ThisIsGeeksforGeeks!, 123" Output :No. of uppercase characters = 4No. of lowercase characters = 15No. of numerical char
2 min read
Find all the patterns of “1(0+)1” in a given string using Python Regex
A string contains patterns of the form 1(0+)1 where (0+) represents any non-empty consecutive sequence of 0’s. Count all such patterns. The patterns are allowed to overlap. Note : It contains digits and lowercase characters only. The string is not necessarily a binary. 100201 is not a valid pattern. Examples: Input : 1101001 Output : 2 Input : 1000
2 min read
Python Regex - Program to accept string starting with vowel
Prerequisite: Regular expression in PythonGiven a string, write a Python program to check whether the given string is starting with Vowel or Not.Examples: Input: animal Output: Accepted Input: zebra Output: Not Accepted In this program, we are using search() method of re module.re.search() : This method either returns None (if the pattern doesn’t m
4 min read
Python Regex | Program to accept string ending with alphanumeric character
Prerequisite: Regular expression in PythonGiven a string, write a Python program to check whether the given string is ending with only alphanumeric character or Not.Examples: Input: ankitrai326 Output: Accept Input: ankirai@ Output: Discard In this program, we are using search() method of re module. re.search() : This method either returns None (if
2 min read
Python regex to find sequences of one upper case letter followed by lower case letters
Write a Python Program to find sequences of one upper case letter followed by lower case letters. If found, print 'Yes', otherwise 'No'. Examples: Input : GeeksOutput : YesInput : geeksforgeeksOutput : NoPython regex to find sequences of one upper case letter followed by lower case lettersUsing re.search() To check if the sequence of one upper case
2 min read
The most occurring number in a string using Regex in python
Given a string str, the task is to extract all the numbers from a string and find out the most occurring element of them using Regex Python. It is guaranteed that no two element have the same frequency Examples: Input :geek55of55geeks4abc3dr2 Output :55Input :abcd1def2high2bnasvd3vjhd44Output :2Approach:Extract all the numbers from a string str usi
2 min read
three90RightbarBannerImg