Check if a string is a valid IP address or not in Python using regular expression

In this post, we will see how we can validate that a given string is a valid IP address (IPv4) or not in Python using regular expression (also knowns as REs or regex or regex pattern). We have already performed this task by the naive approach and now it’s time to step up.
In simple words, Regular Expressions are a sequence of characters used to match the pattern within a string. It has many practical applications and the most known one is the find and replace function in our text editors.
The post will prove to be helpful for those who are willing to start with REs and also for those practicing competitive programming.

Python program to check if a string is a valid IP address or not

In Python, to implement regular expression, firstly we need to import re module. We can import it simply by

import re

Now, we will input the string to be validated and then create the regex pattern. Before creating a regular expression pattern we need to first look at the valid IPv4 format.

Valid IP

We also need to know about some metacharacters to accomplish this task.
1) [ ] – It will match a class of characters given inside the square bracket.
2) –  – It is used for specifying a range within a character class.
Example:- [0-9]  will match any digit from 0 to 9.
[0-91]  will match any digit from 0 to 9 and the last character as ‘1’ instead of any number in range 0 to 92.
                       [a-z]  will match any lowercase alphabet.
3) \ – It acknowledges the interpreter about the special sequence and is also used for escaping a metacharacter.
4) – It works as or operation i.e. matches to a single one of the given patterns.
5) ( ) – It is used to capture a match or to group a pattern.


Regex Pattern

input_string = input("Enter test string:-")
regex_pattern = "^([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$"

In our regex_pattern, we have started with “^” and ended with “$” metacharacters, known as anchors. These are used to ensure that the given input_string starts and ends with the given pattern. Hence, ensuring the test_string is only an IP address. Look into the below image for better understanding,
Anchors Use
“\.” is used to match (.) as it is also a metacharacter used to match anything except the newline character.
To match each numerical element of the IP address we have used the following expression
([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])
*Note:- Remember the pattern should not have space as it is
also a character.

Now we will use a re-module in-built match() function to validate our input_string.
match() function takes regex_pattern and input_string as arguments and returns match object or None if not found.

result = bool( re.match( regex_pattern, inout_string ) )
if (result):
    print("Valid")
else:
    print("Invalid")

The result will be either True or False, which is used for final output.
Let’s have a look at complete code,

import re
input_string = input()
regex_pattern = "^([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$"
result = bool( re.match( regex_pattern, input_string))
if (result):
    print("Valid")
else:
    print("Invalid")

OUTPUT:-

123.64.2.164
Valid
123.264.2.164
Invalid
0.255.255.0
Valid
123.64.2.164IP
Invalid
123.64.  2.164
Invalid

Leave a Reply

Your email address will not be published. Required fields are marked *