Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Regex Python

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

Regex Python

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

Write a Python program to check the validity of passwords input by users.

Validation :
 At least 1 letter between [a-z] and 1 letter between [A-Z].
 At least 1 number between [0-9].
 At least 1 character from [$#@].
 Minimum length 6 characters.
 Maximum length 16 characters.
Program:
import re

def is_valid_password(password):
pattern = re.compile(r"^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[$#@]).{6,16}$")
return bool(pattern.match(password))

# Get password input from the user


password_input = input("Enter your password: ")

# Check the validity of the password


if is_valid_password(password_input):
print("Password is valid.")
else:
print("Password is invalid. Please make sure it meets the specified criteria.")

Mobile Number validation criteria:


 The first digit should contain numbers between 6 to 9.
 The rest 9 digit can contain any number between 0 to 9.
 The mobile number can have 11 digits also by including 0 at the starting.
 The mobile number can be of 12 digits also by including 91 at the starting
The number which satisfies the above criteria is a valid mobile Number.
Program:
import re

def isValid(s):
# 1) Begins with 0 or 91
# 2) Then contains 6,7 or 8 or 9.
# 3) Then contains 9 digits
Pattern = re.compile("(0|91)?[6-9][0-9]{9}")
return Pattern.match(s)

# Driver Code
s = "347873923408"
if (isValid(s)):
print("Valid Number")
else:
print("Invalid Number")

Write a Python program and get an input as a string to validate email that uses regular
expressions to validate if the given email is in a valid email format. The program should
return True for valid emails and False for invalid ones.
Sample Data:
Input:
"user@example.com" # True
"invalid.email@com" # False
"another@123.456" # False

Program:
import re

def validate_email(email):
# Define the regular expression pattern for a valid email
pattern = r'^[a-zA-Z0-9_.+-]+@[a-zA-Z]+\.[a-zA-Z]+$'

# Use re.match to check if the email matches the pattern from the beginning
match = re.match(pattern, email)

# Return True if the email is valid, and False otherwise


return bool(match)

# Sample data
emails = [
"user@example.com", # True
"invalid.email@com", # False
"another@123.456" # False
]

# Validate each email and print the result


for email in emails:
result = validate_email(email.strip())
print(f'Email: "{email}" - Valid: {result}')

Pascal Triangle n=5


n=5 str=""
for i in range(n + 1,2,-1):
for i in range(1, n + 1): for j in range(1, i + 1):
for j in range(0, n - i+1 ): if j==1 or i-j==0:
print(' ', end='') str = "$"
else:
C=1 str = "*"
for j in range(1, i + 1): print(str, end=' ')
print(' ', C,sep='', end='')
C = C * (i - j) // j print()
#print(C) $****$
print() $***$
1 $**$
11 $*$
121
1331
14641

rows = 5 rows = 5
b=5 b=0
for i in range(rows, 0, -1): # reverse for loop from 5 to 0
for i in range(rows, 0, -1):
for j in range(1, i + 1): b += 1
print(b, end=' ') for j in range(1, i + 1):
print('\r') print(b, end=' ')
print('\r')
55555 11111
5555 2222
555 333
55 44
5 5
for i in range(1,rows+1): for i in range(1,rows+1):

for j in range(0, i): for j in range(i, 0,-1):


print(i*2-1, end=' ') print(j, end=' ')
print('\r') print('\r')
1 1
33 21
555 321
7777 4321
99999 54321
for i in range(1,5): s=0
for i in range(1,5):
for j in range(0,i): for j in range(0,i):
print(2*i-j-1, end=' ') s=s+1
print('\r') print(s, end=' ')
1 print('\r')
32 1
543 23
7654 456
7 8 9 10
a=1 def contalpha(n):
b=4 num = 65
k=0 for i in range(0, n):
for i in range(1,b+1): for j in range(0, i + 1):
if i==1: ch = chr(num)
print(i,end=' ') print(ch, end=" ")
else: num = num + 1
k=a+i
for j in range(k,a,-1): print("\r")
print(j, end=' ')
a=k n=5
print() contalpha(n)
1 A
32 BC
654 DEF
10 9 8 7 GHIJ
KLMNO
#Diamond pattern
def fun(r):
row = int(input('Enter number of row: ')) for i in range(1,r+1):
b = 65
# Upper part of diamond for j in range(1,r-i+1):
for i in range(1, row+1): print(" ", end=" ")
for j in range(1,row-i+1):
print(" ", end="") for j in range(1,i+1):
for j in range(1, 2*i): print(" ",chr(b),end=" ")
print("*", end="") b=b+1
print()
print()
# Lower part of diamond
for i in range(row-1,0, -1): for i in range(r+1,1,-1):
for j in range(1,row-i+1): b = 65
print(" ", end="") for j in range(r-i+1,1,-1):
for j in range(1, 2*i): print(" ", end=" ")
print("*", end="")
print() for j in range(i,1,-1):
Enter number of row: 5 print(" ",chr(b),end=" ")
* b=b+1
***
***** print()
******* A
********* A B
******* A B C
***** A B C D
*** A B C D E
* A B C D E
A B C D
A B C
A B
A
Check for other patterns
https://www.codesansar.com/python-
programming-examples/cross-star-
pattern.htm

You might also like