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

Pattern matching with Regex

import re

phoneNumRegex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')

mo = phoneNumRegex.search('My number is 415-555-4242.')

print('Phone number found: ' + mo.group())



Program to accept string ending with alphanumeric character

import re

regex = '[a-zA-z0-9]$'

def check(string):

if(re.search(regex, string)):

print("Accept")

else:

print("Discard")

if __name__ == '__main__' :

string = "python@"

check(string)

string = "python326"

check(string)

string = "python."

check(string)

string = "python"

check(string)


Regex to extract maximum numeric value from a string

import re

def extractMax(input):

numbers = re.findall('\d+',input)

numbers = map(int,numbers)

print (max(numbers))

if __name__ == "__main__":

input = '100klh564abc365bg'

extractMax(input)


Check if an URL is valid or not using Regular Expression

import re

def isValidURL(str):

regex = ("((http|https)://)(www.)?" +

"[a-zA-Z0-9@:%._\\+~#?&//=]" +

"{2,256}\\.[a-z]" +

"{2,6}\\b([-a-zA-Z0-9@:%" +

"._\\+~#?&//=]*)")

p = re.compile(regex)

if (str == None):

return False

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

return True

else:

return False

url = "https://www.pythonforengineers.in"

if(isValidURL(url) == True):

print("Yes")

else:

print("No")


program to check the validity of a Password

import re

password = "D@m@_u0rtu9e$"

flag = 0

while True:

if (len(password)<8):

flag = -1

break

elif not re.search("[a-z]", password):

flag = -1

break

elif not re.search("[A-Z]", password):

flag = -1

break

elif not re.search("[0-9]", password):

flag = -1

break

elif not re.search("[_@$]", password):

flag = -1

break

elif re.search("\s", password):

flag = -1

break

else:

flag = 0

print("Valid Password")

break

if flag ==-1:

print("Not a Valid Password")


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