Python Regex match

How to import re in python?

import re

import re          
nameage = ''' Joy is 22 and Manish is 33
Gabritl is 44 and Ramesh is 21'''
print(nameage)

Output :

Joy is 22 and Manish is 33
Gabritl is 44 and Ramesh is 21

String findall python

How to find only Age from the namesage?

ages = re.findall(r'\d{1,3}', nameage)
print(ages)

Output :

[’22’, ’33’, ’44’, ’21’]

Click Here for – Machine Learning Tutorial

How to find only names?

names = re.findall(r'[A-Z][a-z]*', nameage)
print(names)

Output :

[‘Joy’, ‘Manish’, ‘Gabritl’, ‘Ramesh’]

How to make a dictionary for separating ages with their names?

print()
agedict ={}
x = 0
for eachname in names:
    agedict[eachname]=ages[x]
    x=x+1
print(agedict)

Output :

{‘Joy’: ’22’, ‘Manish’: ’33’, ‘Gabritl’: ’44’, ‘Ramesh’: ’21’}

How to Search in Regex?


if re.search('inform', 'we find some information'):
    print('inform available')

Output :

inform available

Find all how many times inform occur print?

find_all_inform = re.findall('inform', 'we find some information about the listening information')
for i in find_all_inform:
    print(i)

Output :

inform
inform


str = 'we find some information about the listening information'
for i in re.finditer('iform',str):
    loctup = i.span()
    print(loctup)
    

Python Regex replace

str = 'sat,hat,mat,pat'
allstring = re.findall('[shmp]at',str)
for i in allstring:
    print(i)

Output :

sat
hat
mat
pat

str = 'sat,hat,mat,pat'
allstring = re.findall('[^h-m]at',str)
for i in allstring:
    print(i)   

Output :

sat
pat

food = 'sat,hat,mat,pat'
regex = re.compile('[h]at')
food = regex.sub('food',food)
print(food)

Click Here for python tutorial – nested_if_else

Output :

sat, food, mat, pat

randstr='''
keep the blue bag
flying high
chelsea
'''
print(randstr)
regex = re.compile('\n')
ranstr = regex.sub(' ',randstr)
print(randstr)

Output :

keep the blue bag
flying high
chelsea

ranstr = '12345'
print("Matches:", len(re.findall('\d{5}',ranstr)))

Output :

Matches: 1

ranstr = '123 1234 12345'
print("Matches:", len(re.findall('\d{4,5}',ranstr)))

Output :

Matches: 2

\w[a-zA-Z0-9_]it includes all alphabets and 0-9 numbers

\W[^a-zA-Z0-9_]it is opposite of \w means it does not include the alphabets and 0-9 numbers

phn = '134-432-1212'
if re.search('\w{3}-\w{3}-\w(4)',phn):
    print('it is a valid phn number')
if re.search('\w{2,20}\s\w{2,20}', 'Shivani'):
    print('it is a valid namer')
email = 'shivani@.com mad@.com @seo.com'
print('Email Matches:', len(re.findall('[\w._%+-]{1,20}@[\w.-]{2,20}.[A-Za=z]{2,3}',email)))

Web scraping using python

import urllib.request
from re import findall

url  ='https://www...........'     #give the url address from where you want access the scraping data

response = urllib.request.urlopen(url)
html = response.read()
htmlstr = html.decode()
pdata = findall("\(d{3}\) \d{3}-\d{4}", htmlstr)        #the phome number is in the format (231) 457-7896

for item in pdata:
print(item)

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top