RX 2
RX 2
Sets
• A set is of characters inside a pair of square brackets [ ] with a special
meaning.
Set Description
[arn] Returns a match where one of the specified characters (a, r, or n) are present
[a-n] Returns a match for any lower case character, alphabetically between a and n
[0123] Returns a match where any of the specified digits (0, 1, 2, or 3) are present
[a-zA-Z] Returns a match for any character alphabetically between a and z, lower case
OR upper case
[+] In sets, +, *, ., |, (), $,{} has no special meaning, so [+] means: return a match
for any + character in the string
Sample Program 1
• Write a program using regular expressions to verify the Registration
number of student is valid or not
Ex: Test Case1 : 15BEC0234 ; Output : Valid
Test Case2 : 20bec0434 ; Output : Valid
Test Case3 : 15BEC234 ; Output : Invalid
import re
s1=input("Enter student regd. no.:")
if re.search('^[0-9][0-9][a-zA-Z]{3}[0-9]{4}$', s1):
print("Valid")
else:
print("Invalid")
Sample Program 2
• Write a program using regular expressions to verify the given mobile
number is valid or not
Ex: Test Case1 : 1234567890 ; Output : Valid
Test Case2 : 123456789 ; Output : Invalid
Test Case3 : 0123456789 ; Output : Invalid
import re
s1=input("Enter mobile no.:")
if re.search('^[1-9][0-9]{9}$', s1):
print("Valid")
else:
print("Invalid")
Sample Program 3
• Write a program using regular expressions to verify the given
PAN(Permanent Account Number) is valid or not
Ex: Test Case1 : ABCDE1234F ; Output : Valid
Test Case2 : ABCDE1234 ; Output : Invalid
Test Case3 : abcde1234f ; Output : Invalid
import re
s1=input("Enter PAN :")
if re.search('^[A-Z]{4}[0-9]{4}[A-Z]$', s1):
print("Valid")
else:
print("Invalid")
Sample Program 4
• Write a program using regular expressions to verify the given number
is an integer or a float.
Ex: Test Case1 : -123 ; Output : Integer
Test Case2 : 0.0124 ; Output : Float
Test Case3 : -123.456a ; Output : Invalid
import re
s1=input("Enter a number:")
if re.search('^-?[0-9]+$', s1):
print("Integer")
else:
if re.search('^-?[0-9]+.[0-9]+$', s1):
print("Float")
else:
print("Invalid")
Sample Program 6
• Write a program using separate the students with name ‘sri’ in a
dictionary.
Ex: Test Case1 : d={ 'srikanth':20, ‘Srinath':30, 'samsri':40,
'sreekar':50, 'sriram':60 }