Question
In addition to searching for occurrences of strings, you can also use Python regular expressions to perform string substitutions. While most word processors will find
In addition to searching for occurrences of strings, you can also use Python regular expressions to perform string substitutions. While most word processors will find and replace identical strings, you can use regular expressions to match similar strings. For example, you have the following strings:
my val
my-val
my_val
my=val
my&val
You really want them all to be myval. Instead of parsing through your information and replacing each individual occurrence, you can use a regular expression to perform the find and replace
The python pattern is
pattern="my[ -_=&]val"
Regular Expression Search Challenge
Using the Python string below to perform a search using a regular expression that you create.
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
Using the findall() method from the re package determine if there are occurrences in search_string
Assign the outcome of the findall() method to a variable called match1
If match1 is not None: a. Print to the console the pattern used to perform the match, followed by the word matched
Otherwise: a. Print to the console the pattern used to perform the match, followed by the words did not match
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started