95.2k views
3 votes
Regular Expression Replace Challenge

In this challenge you will use the file regex_replace_challenge_student.py to:
Write a regular expression that will replace all occurrences of:
regular-expression
regular:expression r
egular&expression
In the string: This is a string to search for a regular expression like regular expression or regular-expression or regular:expression or regular&expression
Assign the regular expression to a variable named pattern
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

User Jacek Cz
by
4.9k points

1 Answer

6 votes

Answer:

Here is the Python program:

import re # module for regular expressions

search_string='''This is a string to search for a regular expression like regular expression or regular-expression or regular:expression or regular&expression''' #string to search for a regular expression

pattern = "regular.expression" #Assigns the regular expression to pattern

substitution="regular expression" #substitute all occurrences of pattern with regular expression string stored in substitution

replace_results = re.sub(pattern,substitution,search_string) # sub() method from the re package to substitute all occurrences of the pattern with substitution

print(replace_results) #Assigns the outcome of the sub() method to this variable

Step-by-step explanation:

This is a string to search for a regular expression like regular expression or regular-expression or regular:expression or regular&expression

search_string='''This is a string to search for a regular expression like regular expression or regular-expression or regular:expression or regular&expression'''

The following statement assigns the regular expression to a variable named pattern .

pattern = "regular.expression"

The following statement is used to substitute the pattern (regular expression) in the search_string by replacing all occurrences of "regular expression" sub-string on search_string.

substitution="regular expression"

The following statement uses re.sub() method to replace all the occurrences of a pattern with another sub string ("regular expression"). This means in search_string, the sub strings like regular expression, regular-expression, regular:expression or regular&expression are replaced with string "regular expression". This result is stored in replace_results variable. Three arguments are passed to re.sub() method:

sub string to replace i.e. pattern

sub string to replace with i.e. substitution

The actual string i.e. search_string

replace_results = re.sub(pattern,substitution,search_string)

The following print statement displays the output of replace_results

print(replace_results)

The output of the above program is:

This is a string to search for a regular expression like regular expression or regular expression or regular expression or regular expression

Regular Expression Replace Challenge In this challenge you will use the file regex-example-1
User Ruidge
by
6.0k points