Regular Expression Practice Questions - ( DS140823 ) - 18-09-2023

  1. Write a program to take mobile_no as input and check whether it is according to below pattern or also it should check that if mobile number should start with either 6,7,8,9, no other number

+91 7654148497
+91 7654148497

3 Likes
# Write a program to take mobile_no as input and check whether it is according to below pattern or also it should 
# check that if mobile number should start with either 6,7,8,9, no other number

# +91 7654148497
# +91 7654148497

import re 
mobile_number = input("Enter your mobile_number:")
res = re.findall(r"^[+91]{3}[ ]{1}[6-9]{1}[0-9]{9}$",mobile_number) 
print(res)
if res:
    print(mobile_number,"is a valid mobile number")
else:
    print(mobile_number, "is a invalid mobile number")
1 Like
import re

data = input("enter your mobile number: ")
res = re.findall(r"^[+91 ]{4}[6-9]{1}[0-9]{9}$",data)
print(res)
if res:
    print("Format is correct")
else:
    print("Format is incorrect")
import re
mobile_number =input("Enter the mobile number: ")
check =re.findall("[+91]{3}[6-9]{1}[0-9]{9}",mobile_number)
print(check)
if check:
    print("Mobile_number formate is correct")
else:
    print("Mobile_number formate is not correct")  

````
import re 
n=input('Enter Mobile number :')  
r=re.findall('[91+][6-9][0-9]{9}',n) 
if r!=None: 
     print('Valid Number')
else:
     print('Not a valid number')
output:-
Enter Mobile number :917892184556
Valid Number
import re

def validate_mobile_number(number):
    pattern = re.findall(r'^\+91 [6-9]\d{9}$',number)
    return pattern

mobile_number = input("Enter the mobile number :")
pattern2=validate_mobile_number(mobile_number)
if len(pattern2)!=0:
    print(pattern2," valid mobile number")
else:
    print(pattern2,": invalid mobile number")
1 Like
# Write a program to take mobile_no as input and check whether it is according to below pattern or also it should check that if mobile number should start with either 6,7,8,9, no other number
# +91 7654148497
# +91 7654148497

import re 
mobile_number = input("Enter your mobile_number:")
res = re.findall(r"^[+91]{3} [6-9]{1}[0-9]{9}$",mobile_number) 
print(res)
if res:
    print(mobile_number,"is valid")
else:
    print(mobile_number, "is not valid")
1 Like
import re

user_input=input("Enter Mobile :")

res=re.findall("^\+91 [6-9]{1}[0-9]{9}",user_input)
print(res)
if res:
    print(user_input,"is valid")
else:
    print(user_input, "is not valid")
1 Like
# Write a program to take mobile_no as input and check whether it is according to below pattern or also it should check that if mobile number should start with either 6,7,8,9, no other number
# +91 7654148497
# +91 7654148497

import re 
mobile_no = int(input("Enter your mobile no :"))
result = re.findall(r"^[+91]{3} [6-9]{1}[0-9]{9}$",mobile_no) 
print(result)
if result:
    print(mobile_no,"is valid")
else:
    print(mobile_no, "is invalid")