Menu

[Solved] Explain Importance Knowing Use Search Using Literal Characters Sequences Ranges Special Ch Q37261793

Explain the importance of knowing how and when to use a searchusing literal characters, for sequences and ranges, and for specialcharacters and wildcards with examples from my code.

My code:

import re

#Paragraph provided for search and replace

lorem_ipsum = ”’Lorem ipsum dolor sit-amet, consecteturadipiscing elit. Phasellus iaculis velit ac nunc interdumtempor.
Ut volutpat elit metus, vel auctor enim commodo at. Nunc quis quamnon ligula ultricies luctus porta id justo.
Quisque dapibus est ut sagittis bibendum. Mauris ullamcorperpellentesque porttitor. Ut sit:amet ex nec dolor gravida
porttitor. Proin at justo finibus justo vestibulum congue.Suspendisse et ipsum et ipsum eleifend dapibus a fermentum
lacus. Vivamus porta nunc eu nisl sagittis, quis vulputate metusdignissim. Integer non fermentum nisl, id vestibulum
elit. Sed euismod vestibulum purus ut porttitor. Integer sit-ametmollis neque. Donec sed lacinia diam, ac finibus
lectus. Mauris tempor ipsum nisl, vitae posuere est lacinia nec.Nam eget euismod odio.”’

#This statement to find the count the non alphanumericcharacters
#in the string assigned to ‘lorem_ipsum’

#set the pattern in the string
#this is the usage of Sequences and Ranges
#We are defining a rang (A to Z)(0 to 9)
pattern=re.compile(‘[^0-9a-zA-Zd]’)
#find the pattern in the string
results=pattern.findall(lorem_ipsum)
#print the lenth of the characters using len() function
print(len(results))

#This function check the string contains the ‘sit-amet’or ‘sit:amet’
#characters in the string assigned to ‘lorem_ipsum’

#set the pattern in the string
#This is the example of Special Characters and Wildcards
#the special character used is pipe | which matches pattern eitherfrom its left or right
pattern = re.compile(‘sit-amet|sit:amet’)
#find the pattern in the string
#Store the output to a variable named ‘occurrance_sit_amet’
occurrences_sit_amet =pattern.findall(lorem_ipsum)

#print the lenth of the characters using len()function
print(len(occurrences_sit_amet))
#Replace sit:amet and sit-amet with sit amet using the subfunction
#Store the output to a variable named ‘replace_results’
replace_results = re.sub(‘sit-amet|sit:amet’,’sitamet’,lorem_ipsum)

#This function, is to get all of the instances of ‘sitamet’ in the string assigned to ‘replace_results’
#set the pattern in the string
#This is the example of Literal Characters
#We are find the literal phrase sit amet
regex=”sit[s]amet”
#Store the output to a variable named’occurrance_sit_amet’

#Store the output to a variable named’occurrance_sit_amet’
occurrence_sit_amet=re.findall(regex,replace_results)
#print the lenth of the characters using len() function
print(len(occurrence_sit_amet))

Expert Answer


Answer to Explain the importance of knowing how and when to use a search using literal characters, for sequences and ranges, and f… . . .

OR


Leave a Reply

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