Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

USING PYTHON In addition to searching for occurrences of strings, you can also use Python regular expressions to perform string substitutions. While most word processors

image text in transcribed

USING PYTHON

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

Complete this code:

import re

#Sentence with phrase to replace sentence='In my string, I have my val, my-val, my_val, my&val and my=val. But what I really want is my--val.'

#Pattern with the expression to replace pattern = "my[ -_&=]val"

#String to use for substitution substitution="my--val"

#Print out the original sentence print(sentence + " ")

#Replace the regular expression with the substitution string match = re.findall(pattern, sentence)

#Print out the results of the match print("Match = %s " % str(match))

#Replace the results replace_results = re.sub(pattern, substitution, sentence)

#Print out the string containing the substitutions print(replace_results)

#given search string

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

pattern = "regular[ -:&]expression" #regular expression created

match1 = re.findall(pattern, search_string) #using findall to find all occurences

print(" ---------Regular Expression Search Challenge-------------- ") #header

#if match1 is not none

if match1:

print("Pattern used: %s "%pattern) #print pattern

print("matched ") #print given word 'matched'

print("Words matched: %s "%str(match1)) #print results. you can remove this line

#if match1 is empty

else:

print("Pattern used: %s " %pattern) #print pattern

print("did not match") #print words 'did not match'

regex_replac... X 1 2 3 4 5 6 import re #sentence with phrase to replace sentence-' In my string, I have my val, my-val, my-val, my&val and my-val. But what I r :Pattern with the expression to replace pattern "my [ --&z]val'' = 8 #String to use for substitution 10 substitution "my-val" :Print out the original sentence 12 13 14 15 print (sentence +"n") #Replace the regular expression with the substitution string match re . findall (pattern, sentence) 18 :Print out the results of the match print ("Match %s " % str(match)) 19 21 23 = #Replace the results replace-results re.sub(pattern, substitution, sentence) :Print out the string containing the substitutions print(replace_results) #given search string search_string-"This is a string to search for a regular expression like regular express pattern- "regular[-&] expression" #regular expression created match re. findau (pattern, search-string) #using findall to find all occurences print("nRegular Expression Search Challengen" #if match! is not none 26 27 30 31 34 35 36 37 38 39f matchl: 40 print ("Pattern used : %s "%pattern) #print pattern regex_replac... X 1 2 3 4 5 6 import re #sentence with phrase to replace sentence-' In my string, I have my val, my-val, my-val, my&val and my-val. But what I r :Pattern with the expression to replace pattern "my [ --&z]val'' = 8 #String to use for substitution 10 substitution "my-val" :Print out the original sentence 12 13 14 15 print (sentence +"n") #Replace the regular expression with the substitution string match re . findall (pattern, sentence) 18 :Print out the results of the match print ("Match %s " % str(match)) 19 21 23 = #Replace the results replace-results re.sub(pattern, substitution, sentence) :Print out the string containing the substitutions print(replace_results) #given search string search_string-"This is a string to search for a regular expression like regular express pattern- "regular[-&] expression" #regular expression created match re. findau (pattern, search-string) #using findall to find all occurences print("nRegular Expression Search Challengen" #if match! is not none 26 27 30 31 34 35 36 37 38 39f matchl: 40 print ("Pattern used : %s "%pattern) #print pattern

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_2

Step: 3

blur-text-image_3

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

Database Systems For Advanced Applications 9th International Conference Dasfaa 2004 Jeju Island Korea March 2004 Proceedings Lncs 2973

Authors: YoonJoon Lee ,Jianzhong Li ,Kyu-Young Whang

2004th Edition

3540210474, 978-3540210474

More Books

Students also viewed these Databases questions