Showing posts with label Regex. Show all posts
Showing posts with label Regex. Show all posts

Validate an IP address using ReGex

import re

def Validate_It(IP):

regex = "(([0-9]|[1-9][0-9]|1[0-9][0-9]|"\




regex1 = "((([0-9a-fA-F]){1,4})\\:){7}"\


p = re.compile(regex)

p1 = re.compile(regex1)

if (, IP)):

return "Valid IPv4"

elif (, IP)):

return "Valid IPv6"

return "Invalid IP"

IP = ""


IP = "fffe:3465:efab:23fe:2235:6565:aaab:0001"


IP = "2F33:12a0:3Ea0:0302"


Program to count uppercase, lowercase, special character and numeric values using Regex

import re

string = "Pythonforengineers !,* 12345"

uppercase_characters = re.findall(r"[A-Z]", string)

lowercase_characters = re.findall(r"[a-z]", string)

numerical_characters = re.findall(r"[0-9]", string)

special_characters = re.findall(r"[, .!?*]", string)

print("The no. of uppercase characters is", len(uppercase_characters))

print("The no. of lowercase characters is", len(lowercase_characters))

print("The no. of numerical characters is", len(numerical_characters))

print("The no. of special characters is", len(special_characters))