You are on page 1of 4

M.

Mukarrum (02-134192-054)

Lab Tasks
1. Implement the functions re.match and re.search.

Input: (rematch)

import re
pattern = '^a.*s$'
test_string = 'abdsfadfrewrewrdfdfdgfss'
result = re.match(pattern, test_string)

if result:
print("Search successful.")
else:
print("Search unsuccessful.")
Output:

Input: (research)

import re
string="This is Irfan here"
x=re.search("^This .*here$",string)
if x:
print("Search successful.")
else:
print("Search unsuccessful.")
Output:
2. Implement the following functions
 Casefold()
 Center()
 Count()
 Endswith()

Input:

import re
string="HELLO! MUKARRAM HERE"
print(string.casefold())
print(string.center(2))
print(string.count('i'))
print(string.endswith('here'))
Output:

3. Write up a code to split input stream on white spaces, punctuators ((,


), [, ], {, }, ., ,) and operators (+, -, *, /, %,&,&&,!,|,||) of python
language.

Input:

import re
def Function():
inp = input("Enter your INTEGERS, CHARACTERS, OPERATORS: ")
print(inp)
tokens = inp.split(' ')
integer = '^[0-9]+$|[0-9]+'
char = "^'[a-zA-Z0-9]+$|[a-zA-Z0-9]+'$"
Operators = ['+','-','/','*','^']
sinput = inp.split('\n')
for line in sinput:
line = line.split(' ')
for l in line:
if(re.search(integer,l)):
print('Integer :' , l)
elif(re.search(char,l)):
print('character: ' , l)
elif(l in Operators):
print('operator: ' , l)

Function()
Output:

4. Write a method to recognize valid integer, char, float, and string constants.

Input:

import re
def TASK1():
inp = input("Enter your text to identify the INTEGERS, CHARACTERS,
STRING,FLOAT: ")
print(inp)
a=(re.findall(r"[-+]?\d*\.\d+|\d+",inp))
integer = '^[0-9]+$|[0-9]+'
string = "^[a-zA-Z0-9]+$|[a-zA-Z0-9]+"
sinput = inp.split('\n')
for b in a:
b=float(b)
if b%1==0:
b=int(b)
print('Integer :' , b)
elif b%1!=0:
print("Float :" , b)
for line in sinput:
line = line.split(' ')
for i in a:
line.remove(i)
for l in line:
if(re.search(string,l)):
if len(l)==1:
print('Character: ' , l)
else:
print('String: ' , l)
TASK1()
Output:

You might also like