0% found this document useful (0 votes)
12 views8 pages

RX 2

The document explains the use of regular expressions, particularly focusing on character sets and their meanings. It provides several sample programs to validate student registration numbers, mobile numbers, PAN numbers, and to identify integers and floats. Additionally, it includes a program to separate students with the name 'sri' in a dictionary.

Uploaded by

shroffyohann2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views8 pages

RX 2

The document explains the use of regular expressions, particularly focusing on character sets and their meanings. It provides several sample programs to validate student registration numbers, mobile numbers, PAN numbers, and to identify integers and floats. Additionally, it includes a program to separate students with the name 'sri' in a dictionary.

Uploaded by

shroffyohann2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Regular Expressions

Sets
• A set is of characters inside a pair of square brackets [ ] with a special
meaning.

• A hyphen between a pair of symbols enclosed in brackets is used to


indicate a range of characters; For example A–Z, a–z, or 0–9 for
uppercase letters, lowercase letters, and numeric digits, respectively.

• A caret symbol between a pair of symbols enclosed in brackets is used


to indicate negation.
Sets

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

[^arn] Returns a match for any character EXCEPT a, r, and n

[0123] Returns a match where any of the specified digits (0, 1, 2, or 3) are present

[0-9] Returns a match for any digit between 0 and 9

[0-5][0-9] Returns a match for any two-digit numbers from 00 and 59

[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 }

You might also like