Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PROMPTS ARE IN BOLD... 1 The first thing to do when doing a pattern search is to import the module re which provides full support

PROMPTS ARE IN BOLD...

1 The first thing to do when doing a pattern search is to import the module re which provides full support for regular expressions in Python. This has already been provided in the code to the left.

Also provided for you on the left is the original string,original_text and the string,lorem_ipsum. lorem_ipsum is the string you will be using to change as you go through the programming activities in this course unit.

Find all of the instances of non alphanumeric characters in the string assigned to lorem_ipsum. Use the ^ and [] regular expression operator along with the findall() regular expression function.

Assign the outcome to a variable named results.

Output to the console, the number of non-alphanumeric characters. Hint: use the len function.

OUTPUT SHOULD BE 144

import re

#Paragraph provided for search and replace

original_text='''Lorem ipsum dolor sit-amet, consectetur adipiscing elit. Phasellus iaculis velit ac nunc interdum tempor. Ut volutpat elit metus, vel auctor enim commodo at. Nunc quis quam non ligula ultricies luctus porta id justo. Quisque dapibus est ut sagittis bibendum. Mauris ullamcorper pellentesque 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 metus dignissim. Integer non fermentum nisl, id vestibulum elit. Sed euismod vestibulum purus ut porttitor. Integer sit-amet mollis neque. Donec sed lacinia diam, ac finibus lectus. Mauris tempor ipsum nisl, vitae posuere est lacinia nec. Nam eget euismod odio.'''

lorem_ipsum = '''Lorem ipsum dolor sit-amet, consectetur adipiscing elit. Phasellus iaculis velit ac nunc interdum tempor. Ut volutpat elit metus, vel auctor enim commodo at. Nunc quis quam non ligula ultricies luctus porta id justo. Quisque dapibus est ut sagittis bibendum. Mauris ullamcorper pellentesque 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 metus dignissim. Integer non fermentum nisl, id vestibulum elit. Sed euismod vestibulum purus ut porttitor. Integer sit-amet mollis neque. Donec sed lacinia diam, ac finibus lectus. Mauris tempor ipsum nisl, vitae posuere est lacinia nec. Nam eget euismod odio.'''

#Using the findall function, get all of the instances of non alphanumeric characters in the string assigned to 'lorem_ipsum' #Output to the console, the number of non-alphanumeric characters. Hint: use the len function

#Using the findall function, get all of the instances of 'sit-amet' or 'sit:amet' characters in the string assigned to 'lorem_ipsum' #Assign the outcome to a variable named 'occurrance_sit_amet'

#Output to the console, the number of sit-amet or sit:amet occurrances. Hint: use the len function

#Replace sit:amet and sit-amet with sit amet using the sub funciton #Assign the outcome to a variable named 'replace_results'

#Using the findall function, get all of the instances of 'sit amet' in the string assigned to 'replace_results' #Assign the outcome to a variable named 'occurrance_sit_amet'

#Output to the console, the number of sit amet occurrances. Hint: use the len function

to_fnd = re.compile('[^ 0-9 a-z A-Z]') result = to_fnd.findall(lorem_ipsum) print('The non-alphanumeric characters can be given by:') print(len(result))

to_fnd1 = re.compile('sit-amet|sit:amet') num =to_fnd1.findall(lorem_ipsum) print('The number of occurrances of the pattern is given by:') print(len(num))

replace_results = re.sub('sit-amet|sit:amet','sit amet',lorem_ipsum) to_fnd2 = re.compile('sit amet') num = to_fnd2.findall(replace_results) print('The number occurrances of the patternafter replacement is given by:') print(len(num))

2

Using the re.findall() function, get all of the instances of 'sit-amet' or 'sit:amet' characters in the string assigned to lorem_ipsum.

Assign the outcome to a variable named occurrences_sit_amet

Output to the console, the number of sit-amet or sit:amet occurrences.

OUTPUT SHOULD BE 3

3

Using the re.findall() function, get all of the instances of 'sit amet'in the string assigned to replace_results.

Assign the outcome to a variable named occurrence_sit_amet.

Output to the console, the number of sit amet occurrences.

OUPUT SHOULD BE 3

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Current Trends In Database Technology Edbt 2004 Workshops Edbt 2004 Workshops Phd Datax Pim P2panddb And Clustweb Heraklion Crete Greece March 2004 Revised Selected Papers Lncs 3268

Authors: Wolfgang Lindner ,Marco Mesiti ,Can Turker ,Yannis Tzitzikas ,Athena Vakali

2005th Edition

3540233059, 978-3540233053

More Books

Students also viewed these Databases questions

Question

1. Are my sources credible?

Answered: 1 week ago

Question

3. Are my sources accurate?

Answered: 1 week ago

Question

1. Is it a topic you are interested in and know something about?

Answered: 1 week ago