Python List Comprehension and Regular Expression
Exercise on List Comprehension
Consider the list below.
name=["Satish", "ram", "John", "Chris", "jeff", "Sam"]
Create another list namecap which contains the list of all names that start with a capital letter from the list given above (name)
Solution
name=["Satish","ram","John","Chris","jeff","Sam"]
namecap=[item for item in name if item[0:1].isupper()]
print(namecap)
Output
Exercise 2 on List Comprehension
Consider the list given below.
k=[[2,3],[5,4]]
Create another list using list comprehension by name evenlist that contains only
even numbers present in list k. Intialize odd numbers
to "". The evenlist should contain output as
[[2, ''], ['', 4]]
Output
Solution
k=[[0,0],[0,0]]
for i in range(len(k)):
for j in range(len(k[i])):
k[i][j]=int(input())
evenlist=[[k[i][j] if k[i][j]%2==0 else "" for j in range(len(k[i]))]for i in range(len(k))]
print(evenlist)
Output
Matching any digit using Python Regular Expression
import re
pat=r"\d"
sentence="Satish teaches Python on Saturdays at 317"
matches=re.search(pat,sentence)
if matches:
print("pattern found")
else:
print("pattern not found")
Output
Matching any non digit character using Python Regular Expression
import re
pat=r"\D"
sentence="Satish teaches Python on Saturdays at 317"
matches=re.search(pat,sentence)
if matches:
print("pattern found")
else:
print("pattern not found")
Output
Matching a meta character using Python Regular Expression
import re
pat=r"\+"
sentence="will Satish test + this code"
matches=re.search(pat,sentence)
if matches:
print("pattern found")
else:
print("pattern not found")
\s example
import re
pat=r"\s"
sentence="Satishtestthis"
matches=re.search(pat,sentence)
if matches:
print("pattern found")
else:
print("pattern not found")
Output
Checking if a word occurs in the beginning of the sentence
import re
pat=r"^Satish"
sentence="Satish will test this code"
matches=re.search(pat,sentence)
if matches:
print("pattern found")
else:
print("pattern not found")
Output
. example code
import re
pat=r"."
sentence="\n"
matches=re.search(pat,sentence)
if matches:
print("pattern found")
else:
print("pattern not found")
Output
Matching the pattern at the end of the sentence
import re
pat=r"code$"
sentence="Satish will test this code"
matches=re.search(pat,sentence)
if matches:
print("pattern found")
else:
print("pattern not found")
Output
Extracting phone without quantifiers
import re
pat=r"\d\d\d\d-\d\d\d\d\d\d\d"
sentence="phone number is 0416-2227200 and 0416-2228730"
matches=re.findall(pat,sentence)
for match in matches:
print(match)
Output
Extracting phone numbers with quantifiers
import re
pat=r"\d{4}-\d{7}"
sentence="phone number is 0416-2227200 and 0416-2228730"
matches=re.findall(pat,sentence)
for match in matches:
print(match)
Output
Extracting all room numbers from the example
import re
pat=r"\d+"
sentence="The room numbers are 316 317 and 318"
matches=re.findall(pat,sentence)
for match in matches:
print(match)
Output
use of ? (one or more quantifier) -extract all names with Dr. before a name from the string
The guests are Dr Sam , Dr.Ram, Dr.Tom and Mr.Jeff
import re
pat=r"Dr\.?\s[A-Z]\w+"
sentence="The guests are Dr. Sam,Dr Ram,Dr. Tom and Mr. Jeff"
matches=re.findall(pat,sentence)
for match in matches:
print(match)
Output
Extracting any other words except rest
import re
pat=r"[^r]est"
sentence="test best rest "
matches=re.findall(pat,sentence)
for match in matches:
print(match)
Output
Extracting land line numbers of vellore from a string
import re
pat=r"0416-2{3}[0-9]{4}"
sentence="phone number is 0416-2224136 and 0416-2223150"
matches=re.findall(pat,sentence)
for match in matches:
print(match)
Output
Validate vit email id test@vit.ac.in,test.cj@vit.ac.in,test214@vit.ac.in
import re
pat=r"\w+\.?\w+@{1}vit\.ac\.in"
sentence="test_cj@vit.ac.in satish@gmail.com satish@yahoo.com"
matches=re.findall(pat,sentence)
for match in matches:
print(match)
Output
Validate any email id
test_12@gmail.com test@aol.net test@vit.edu
#validate any email id .com .edu ,.net
import re
pat=r"\w+@\w+.(?:com|edu|net)"
sentence=" test_12@yahoo.com test@vit.edu test@aol.net"
matches=re.findall(pat,sentence)
for match in matches:
print(match)
Output
Validate an URL using Python Regulare Expression
import re
pat=r"https?://[www\.]?\w+\.\w+"
sentence="""
http://test.com
https://www.youtube.com
http://www.test.com"""
matches=re.findall(pat,sentence)
for match in matches:
print(match)
Output
validating password using Python Regular Expression
#validate password
import re
password=input()
pat=r"\d"
matches=re.search(pat,password)
if matches==None:
print("Password should contain a digit")
pat1="[a-z]"
matches=re.search(pat1,password)
if matches==None:
print("Password should contain atleast one lower case letter")
pat2="[A-Z]"
matches=re.search(pat2,password)
if matches==None:
print("Password should contain atleast one upper case letter")
pat3=r"[$#@_]"
matches=re.search(pat3,password)
if matches==None:
print("Password should contain atleast one special character")
if len(password)<6:
print("password should not be less than 6 characters in length")
Output