Open In App

Validate an IP address using Python without using RegEx

Last Updated : 31 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 of four numbers (each between 0 and 255) separated by periods. The format of an IP address is a 32-bit numeric address written as four decimal numbers (called octets) separated by periods; each number can be written as 0 to 255 (E.g. – 0.0.0.0 to 255.255.255.255).

Examples: 

Input:  192.168.0.1
Output: Valid IP address

Input: 110.234.52.124
Output: Valid IP address

Input: 666.1.2.2
Output: Invalid IP address

Validate an IP address using Python without using RegEx

Method #1: Checking the number of periods using count() method and then the range of numbers between each period.

Python3




# Python program to verify IP without using RegEx
 
# explicit function to verify IP
def isValidIP(s):
 
    # check number of periods
    if s.count('.') != 3:
        return 'Invalid Ip address'
 
    l = list(map(str, s.split('.')))
 
    # check range of each number between periods
    for ele in l:
        if int(ele) < 0 or int(ele) > 255 or (ele[0]=='0' and len(ele)!=1):
            return 'Invalid Ip address'
 
    return 'Valid Ip address'
 
 
# Driver Code
print(isValidIP('666.1.2.2'))


Output

Invalid Ip address

Method #2: Using a variable to check the number of periods and using a set to check if the range of numbers between periods is between 0 and 255(inclusive).

Python3




# Python program to verify IP without using RegEx
 
# explicit function to verify IP
def isValidIP(s):
 
    # initialize counter
    counter = 0
 
    # check if period is present
    for i in range(0, len(s)):
        if(s[i] == '.'):
            counter = counter+1
    if(counter != 3):
        return 0
 
    # check the range of numbers between periods
    st = set()
    for i in range(0, 256):
        st.add(str(i))
    counter = 0
    temp = ""
    for i in range(0, len(s)):
        if(s[i] != '.'):
            temp = temp+s[i]
        else:
            if(temp in st):
                counter = counter+1
            temp = ""
    if(temp in st):
        counter = counter+1
 
    # verifying all conditions
    if(counter == 4):
        return 'Valid Ip address'
    else:
        return 'Invalid Ip address'
 
 
# Driver Code
print(isValidIP('110.234.52.124'))


Output

Valid Ip address



Previous Article
Next Article

Similar Reads

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 the type of IP Address using Regex
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
2 min read
Python program to validate an IP Address
Prerequisite: Python Regex Given an IP address as input, write a Python program to check whether the given IP Address is Valid or not. 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 of four
4 min read
How to validate MAC address using Regular Expression
Given string str, the task is to check whether the given string is a valid MAC address or not by using Regular Expression. A valid MAC address must satisfy the following conditions: It must contain 12 hexadecimal digits.One way to represent them is to form six pairs of the characters separated with a hyphen (-) or colon(:). For example, 01-23-45-67
6 min read
Regular Expression to Validate a Bitcoin Address
BITCOIN is a digital currency. During the digital currency transaction, a BTC address is required to verify the legality of a bitcoin wallet a Bitcoin address is a long set of alphanumeric characters that contains numbers and letters. A Bitcoin address indicates the source or destination of a Bitcoin payment. A Bitcoin wallet is a digital wallet th
6 min read
Program to validate an IP address
Write a program to Validate an IPv4 Address. According to Wikipedia, IPv4 addresses are canonically represented in dot-decimal notation, which consists of four decimal numbers, each ranging from 0 to 255, separated by dots, e.g., 172.16.254.1 Recommended PracticeValidate an IP AddressTry It!Following are steps to check whether a given string is a v
15+ min read
Program to check the validity of password without using regex.
Password checker program basically checks if a password is valid or not based on the password policies mention below: Password should not contain any space. Password should contain at least one digit(0-9). Password length should be between 8 to 15 characters. Password should contain at least one lowercase letter(a-z). Password should contain at lea
9 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 | 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
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