Question: Project 4 Draft: Pattern Search Introduction Your task for this project is to use regular expressions to discover and modify information from the provided test.

Project 4 Draft: Pattern Search

Introduction

Your task for this project is to use regular expressions to discover and modify information from the provided test. The project is broken into two sections:

Determine the number of non alpha numeric characters in the lorem_ipsum string.

Replace all instances of the words sit and amet separated by a dash (-) or a colon (:) with a space.

--------------------------------------------------------------------------------

Find and print the number of non-alphanumeric characters.

Hint: use the len function.

Output

144

Number of non-alphanumeric characters.

Check It!

LAST RUN on 12/8/2017, 5:35:00 PM

Check 1 passed ( I got this to pass) 

----------------------------------------------------------------------------------

Find and print the number of 'sit' and 'amet' separated with punctuation marks

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

2. Assign the outcome to a variable named occurrences_sit_amet

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

Hint: use the len function.

Output

3

Check 1 failed

Output: 

144 3 779

Expected:

3

-----------------------------------------------------------------------------------

Replace 'sit' and 'amet' words separated with punctuation marks with 'sit amet'

1. Replace sit:amet and sit-amet with sit amet using the re.sub() function.

2. Assign the outcome to a variable named replace_results

--------------------------------------------------------------------------------------------

Find and print all the instances of 'sit amet'

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

2. Assign the outcome to a variable named occurrence_sit_amet.

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

Hint: use the len function.

Output

3

Check 1 failed

Output: 

144 3 779

Expected:

3

----------------------------------------------------------------------------------

My code (Python e2, Codio)

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

results = re.findall(r'[^a-zA-Z]', lorem_ipsum)

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

results = re.findall(r'sit[-|:]amet', lorem_ipsum)

print(len(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

results = re.sub(r'sit[-|:]amet', r'sit amet', lorem_ipsum)

print(len(results))

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!