Practice Set 1 - Module-2
Practice Set 1 - Module-2
Problem
Matched
The expression used in the example, matches any
character for ‘.’
import re
if re.match(".end","bends"):
print("Matched")
else:
print("Not matched")
Prints Matched
import re
if re.match(".end$","bends"):
print("Matched")
else:
print("Not matched“)
Prints Not matched - $ check for end of string
Matching from the Beginning or End of Strings or
Word Boundaries (^, $)
^ - Match beginning of string
$ - Match End of string
if you wanted to match any string that ended
with a dollar sign, one possible regex solution
would be the pattern .*\$$
But not sufficient
Check whether the given register number of a VIT
student is valid or not.
Example register number – 15bec1032
Register number is valid if it has two digits
Followed by three letters
Followed by four digits
Denoting Ranges (-) and Negation (^)
• brackets also support ranges of characters
• 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
Multiple Occurrence/Repetition Using Closure
Operators (*, +, ?, {})
• special symbols *, +, and ?, all of which can be
used to match single, multiple, or no occurrences
of string patterns
• Asterisk or star operator (*) - match zero or more
occurrences of the regex immediately to its left
• Plus operator (+) - Match one or more
occurrences of a regex
• Question mark operator (?) - match exactly 0 or 1
occurrences of a regex.
• There are also brace operators ({}) with either a
single value or a comma-separated pair of values.
These indicate a match of exactly N occurrences
(for {N}) or a range of occurrences; for example,
{M, N} will match from M to N occurrences
Code to check the validity of register number
import re
register= input()
if re.match("^[1-9][0-9][a-zA-Z][a-zA-Z][a-zA-Z][0-
9][0-9][0-9][0-9]$",register):
print("Matched")
else:
print("Not matched“)
^ - denote begin (Meaning is different when we put
this symbol inside the square bracket)
$ - denote end
Refined Code to check the validity of register
number
{n} – indicate that the pattern before the braces
should occur n times
import re
register= input()
if re.match("^[1-9][0-9][a-zA-Z]{3}[0-
9]{4}$",register):
print("Matched")
else:
print("Not matched")
Check validity of Mobile Number (Shorter Code)
import re
number = input()
if re.match(‘[^0][0-9]{9}',number):
print('valid')
else:
print('invalid')
Bug: Will also accept a843338320
Check validity of Mobile Number (Shorter Code)
import re
number = input()
if re.match(‘[1-9][0-9]{9}',number):
print('valid')
else:
print('invalid')
Check validity of PAN card number with RE
import re
pan=input()
if len(pan) < 10 and len(pan) > 10 :
print ("PAN Number should be 10 characters")
exit
elif re.search("[^a-zA-Z0-9]",pan):
print ("No symbols allowed, only
alphanumerics")
exit
elif re.search("[0-9]",pan[0:5]):
print ("Invalid - 1")
exit
elif re.search("[A-Za-z]",pan[5:9]):
print ("Invalid - 2")
exit
elif re.search("[0-9]",pan[-1]):
print ("Invalid - 3")
exit
else:
print ("Your card "+ pan + " is valid“)
Python read all input as string
In some cases it is necessary to check if the value
entered is an integer
We can check it using regular expressions
Rules for an integer
optionally begin with a negative sign include ^
symbol
first digit must be a number other than zero
may be followed zero to any number of digits
string must end with it so add $ symbol
import re
register= input()
#optionally begin with a negative sign include ^
symol
#first digit must be a number other than zero
# may be followed zero to any number of digits
# string must end with it so add $ symbol
if re.match("^\-?[1-9][0-9]*$",register):
#'\' is added in front of '-' to overcome its default
meaning in REs
print("Matched")
else:
print("Not matched")
Rules for an integer or a floating point value
optionally begin with a negative sign include ^
symbol
first digit must be a number other than zero
may be followed zero to any number of digits
string must end with it so add $ symbol
Optionally followed by a ‘.’
Followed by zero or more digits
String ends here
import re
register= input()
if re.match("^\-?[1-9][0-9]*\.?[0-9]*$",register):
# ’.’ can occur zero or one time followed by a
digit occurred zero to infinite number of times
print("Matched")
else:
print("Not matched")