Before going to learn this program first you know about what is a regular expression? and How you can create your pattern? That was very easy and interesting, you will practice daily you can easily understand the create custom pattern.
In python programming language we use the (re) package, that package will be used to create patterns easily, let star that
Regular expressions can be defined as the sequence of characters that are used to search for a pattern in a string. The module re provides the support to use regex in the python program. The re module throws an exception if there is some error while using the regular expression.
Regular Expression is widely used in the UNIX world.
Where regular expressions are exactly used?
Data mining, Data validations, NLP social media platforms, Bank activities and other operations.
Regex Functions:
S.No | Function | Description |
---|---|---|
1 | match | This method matches the regex pattern in the string with the optional flag. It returns true if a match is found in the string otherwise it returns false. |
2 | findall | It returns a list that contains all the matches of a pattern in the string. |
3 | search | This method returns the match object if there is a match found in the string. |
4 | sub | Replace one or many matches in the string |
5 | split | .Returns a list in which the string has been split in each match. |
Pattern = “^[_A-Za-z0-9]+(\.[_A-Za-z0-9]+)*@[A-Za-z0-9]+(\.[A-Za-z0-9]+*(\.[A-Za-z]{2,4})$”
Program in python:(1)
import re #Import Package
def Check_MyEmail(My_Pattern): #Function Call
while True:
Email=input(“Enter the E-Mail : “)
if not re.match(My_Pattern,Email):
print(“your pattern not matched”)
elif not My_Pattern:
print(“Forgot to enter the pattern”)
else:
print(“E-mail is veryfied!”)
break
Pattern = “^[_A-Za-z0-9]+(\.[_A-Za-z0-9]+)*@[A-Za-z0-9]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,4})$”
print(Check_MyEmail(Pattern)) #Function Call
Sample Output:
Test case 1:
Enter the E-Mail : innovativecodesacacademy1@gmail.com
E-mail is verified!
None
Test case 2:
Enter the E-Mail: innovativecodesacacademy1@gmail.ac.in
E-mail is verified!
None
Test case 3:
Enter the E-Mail : innovativecodesacacademy1gmail.ac.in
your pattern not matched
Program in python:(2)
import re
def Check_MyEmail(My_Pattern):
while True:
Email=input(“Enter the E-Mail : “)
if re.match(My_Pattern,Email):
print(“E-mail is veryfied!”)
else:
print(“Invalid Email! 🙁 “,Email)
break
Pattern=”^[A-Za-z0-9.%+-]+@[A-Za-z0-9.-]+(.[A-Za-z]{2,4})$”
print(Check_MyEmail(Pattern))
Sample Output:
Test case 1:
Enter the E-Mail : innovativecodesacacademy1@gmail.com
E-mail is verified!
Test case 2:
Enter the E-Mail : innovativecodesacacademy1@gmail.ac.in
Invalid Email! 🙁 innovativecodesacacademy1@gmail.ac.in
None
In the next blog, We will explain how to create the pattern for complex e-mail patterns creation and verification? the purpose for each character.