Question: Regular Expression Replace Challenge The Python statement containing the string to search for the regular expression occurrence is below. search_string=This is a string to search
Regular Expression Replace Challenge
The Python statement containing the string to search for the regular expression occurrence is below. search_string=This is a string to search for a regular expression like regular expression or regular-expression or regular:expression or regular&expression
Write a regular expression that will find all occurrences of: a. regular expression b. regular-expression c. regular:expression d. regular&expression in search_string
Assign the regular expression to a variable named pattern
The Python string below is used for substitution substitution="regular expression"
Using the sub() method from the re package substitute all occurrences of the pattern with substitution
Assign the outcome of the sub() method to a variable called replace_result
Output to the console replace_results
CODE GIVEN:
import re
#The string to search for the regular expression occurrence (This is provided to the student) search_string='''This is a string to search for a regular expression like regular expression or regular-expression or regular:expression or regular&expression'''
#1. Write a regular expression that will find all occurrances of: # a. regular expression # b. regular-expression # c. regular:expression # d. regular&expression # in search_string #2. Assign the regular expression to a variable named pattern
#The string to use for subsitution (This is provided to the student) substitution="regular expression"
#3. Using the sub() method from the re package substitute all occurrences of the 'pattern' with 'substitution' #4. Assign the outcome of the sub() method to a variable called replace_result
#5. Output to the console replace_results
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
