Sunday 26 June 2022

P#32 Reg expression

To find a pattern in a statement

match = re.search('love', 'i love you all')

if match:
print('found love')
else:
print('love Not found')

# found love
match = re.search('love', 'i leave you all')

if match:
print('found love')
else:
print('love Not found')

# love Not found

 ## . = any char but \n
match = re.search(r'..ve', 'i love you all') # found,
print(match)

To match email

 to check up Google Account
str = 'hello I am so& so i have account you@google.com'
match = re.search(r'\w+@\w+', str)
if match:
print(match.group()) #you@google

match = re.search(r'[\w.-]+@[\w.-]+', str)
if match:
print(match.group()) ## 'you@google.com'

To Validate IFSC code in banks

import re
def isValidIFSCode(str):
# Regex to check valid IFSC Code.
regex = "^[A-Z]{4}0[A-Z0-9]{6}$" # 4 Alphabets,0,
# Compile the ReGex
p = re.compile(regex)
# If the string is empty
# return false
if (str == None):
return False
# Return if the string
# matched the ReGex
if (re.search(p, str)):
return True
else:
return False
# Driver code
# Test Case 1:
str1 = "SBIN0125620"
print(isValidIFSCode(str1))

# Test Case 2:
str2 = "SBIN0125"
print(isValidIFSCode(str2))

# Test Case 3:
str3 = "1234SBIN012"
print(isValidIFSCode(str3))

# Test Case 4:
str4 = "SBIN7125620"
print(isValidIFSCode(str4))

# This code is contributed by avanitrachhadiya2155

"""
True
False
False
False
"""

Please Note:


# regex = "^[A-Z]{4}0[A-Z0-9]{6}$";
# Where:
# ^ represents the starting of the string.
# [A-Z]{4} represents the first four characters should be upper case alphabets.
# 0 represents the fifth character should be 0.
# [A-Z0-9]{6} represents the next six characters usually numeric, but can also be alphabetic.
# $ represents the ending of the string.

To test Pin Code

import re

def isValidPinCode(pinCode):
# Regex to check valid pin code
# of India.
regex = "^[1-9]{1}[0-9]{2}\\s{0,1}[0-9]{3}$";

# Compile the ReGex
p = re.compile(regex);

# If the pin code is empty
# return false
if (pinCode == ''):
return False;

# Pattern class contains matcher() method
# to find matching between given pin code
# and regular expression.
m = re.match(p, pinCode);

# Return True if the pin code
# matched the ReGex else False
if m is None:
return False
else:
return True



# check Driver code
if __name__ == "__main__":
# Test case 1
num1 = "625001";
print(num1, ": ", isValidPinCode(num1));

# Test case 2:
num2 = "625 001";
print(num2, ": ", isValidPinCode(num2));

# Test case 3:
num3 = "0625 001";
print(num3, ": ", isValidPinCode(num3));

# Test case 4:
num4 = "6250001";
print(num4, ": ", isValidPinCode(num4));

Result


"""
625001 : True
625 001 : True
0625 001 : False
6250001 : False
"""

To check PANCARD in India


# To Valiate pan Card

import re

def checkPanCard():
import re


def isValidPanCardNo(panCardNo):

# PAN Card number
regex = "[A-Z]{5}[0-9]{4}[A-Z]{1}"

# Compile the ReGex
p = re.compile(regex)

# If the PAN Card number
# is empty return false
if(panCardNo == None):
return False

# Return if the PAN Card number
# matched the ReGex
if(re.search(p, panCardNo) and
len(panCardNo) == 10):
return True
else:
return False

# Driver Code.


# Test Case 1:
str1 = "AFGHA2318J"
print(isValidPanCardNo(str1),' ',str1)

# Test Case 2:
str2 = "23AFGHN18J"
print(isValidPanCardNo(str2),str2)

# Test Case 3:
str3 = "AFGHA2318JM"
print(isValidPanCardNo(str3),str3)

# Test Case 4:
str4 = "AFGHA23184"
print(isValidPanCardNo(str4),str4)

# Test Case 5:
str5 = "AFGHA 23184"
print(isValidPanCardNo(str5),str5)

checkPanCard()
"""
True AFGHA2318J
False 23AFGHN18J
False AFGHA2318JM
False AFGHA23184
False AFGHA 23184
"""


No comments:

Post a Comment

Making Prompts for Profile Web Site

  Prompt: Can you create prompt to craft better draft in a given topic. Response: Sure! Could you please specify the topic for which you...