How we can Implement regular expression in python

Can u explain the Re and important functions related to Re module? With any 2 Examples.

Regular Expressions:
if we want to represent a group of strings , according to a particular pattern then
we should use Regular Expression.

Example of RE → fixed pattern is there for our mobile numbers
Example of RE → Fixed pattern is given to our mail id
Example of RE → fixed pattern is for vechile or mobile number
Example of RE --:> PAN CARD
Example of RE → Strong password, weak password , and medium password

import re
x=input(“Enter your Mail Id”)
matcher = re.finditer(“@”,x)
print(matcher)
for match in matcher:
y = match.start()
if x[y+1:] == “gmail.com”:
print(“It is a google account”)
elif x[y+1:] == “yahoo.com”:
print(“It is a yahoo account”)
elif x[y+1:] == “hotmail.com”:
print(“It is a hotmail account”)
else:
print(“Please enter valid mail id”)

if you enter parkar@gmail.com as input, it will return it is a google acoount

1 Like

A regular expression (or RE) specifies a set of strings that matches it; the functions in this module let you check if a particular string matches a given regular expression (or if a given regular expression matches a particular string, which comes down to the same thing).

The re module provides a set of powerful regular expression facilities, which allows you to quickly check whether a given string matches a given pattern (using the match function), or contains such a pattern (using the search function).

Important Functions are :-

  1. Findall()
  2. Search()
  3. Split()
  4. Sub()

Examples: -

image

image

2 Likes