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]|"\

"2[0-4][0-9]|25[0-5])\\.){3}"\

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

"2[0-4][0-9]|25[0-5])"

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

"([0-9a-fA-F]){1,4}"

p = re.compile(regex)

p1 = re.compile(regex1)

if (re.search(p, IP)):

return "Valid IPv4"

elif (re.search(p1, IP)):

return "Valid IPv6"

return "Invalid IP"

IP = "203.120.223.13"

print(Validate_It(IP))

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

print(Validate_It(IP))

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

print(Validate_It(IP))


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))