Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#!/usr/bin/python import re # Regular expression for detecting patterns # TODO: Fill in the quotes with a python regular expression that represents the required strings.

image text in transcribed

#!/usr/bin/python

import re

# Regular expression for detecting patterns # TODO: Fill in the quotes with a python regular expression that represents the required strings.

myRE1 = '\w*[qzQZ]+\w+' # RE for strings that contain a 'q' or a 'z' (case-insensitive) myRE2 = "(?:is|IS|Is|iS)" # RE for strings that contain a sequence 'is' (case-insensitive) myRE3 = '' # RE for "email addresses" as defined in HW2 specification myRE4 = '' # RE for "URL" as defined in HW2 specification

print "Have detected the following strings for regular expression 1:" with open("sample.txt","r") as f: for line in f: myDetections = re.findall(myRE1,line) for str in myDetections: print "\t", str

print " Have detected the following strings for regular expression 2:" with open("sample.txt","r") as f: for line in f: myDetections = re.findall(myRE2,line) for str in myDetections: print "\t", str

#print " Have detected the following strings for regular expression 3:" #with open("sample.txt","r") as f: # for line in f: # myDetections = re.findall(myRE3,line) # for str in myDetections: # print "\t", str

#print " Have detected the following strings for regular expression 4:" #with open("sample.txt","r") as f: # for line in f: # myDetections = re.findall(myRE4,line) # for str in myDetections: # print "\t", str

Sprint 5:56 PM 23%) HW2 Spring2019 PDF-51 KB Close For this problem,you will construct Python Regular Expressions You are givena Python script called "problem2py.You have to construct a regular expression for each of these types of strings (patterns) 1. Amy string that has a cora'(case insensitive, Le upper or lowercase'o (5 points 2. Amy string that contains the sequence 'is' in the word (case insensitive as well) (S points) . Cemail) Any string that has the foBowing properties (15 points) a Has an symbol b Before the symbol, the string can only have one or more alpha numeric characters. This string before the'@can also periods, but it cannot have more than one consecutive period and it cannot end or begin with a period C. The string after the 'symbol can have alphanumeric characters and periods. However, this part ofthe string cannot have consecutive periods, and it cannot start or end with a period Also, this string is required to have at least one period. (Hint: In Python expressions, the needs to be escaped with al he-in regular expressions in Python means any character) CURLAny string that has the following properties: (15 peints) a Begins with the sequence "http/ b Is followed by astring of alphanumeric characters and periods However, this string cannot start or end in a period and cannot have consecutive periods Then can (but doesn't necessanily have to) be followed by a string that starts with a 'f and can have any number of alpha numeric characters or forward slashes C. All you will have to do is fill in the empty quotes in the python script file, in the lines that say YRE1 Whenever you fill out your expressions, if you want to test the strings that it matches, you can uncomment the appropriate block that you want to test (removing In the Blackboard, we are including a sample test file called sample.te. We are also including a file called your program should have for this sample file that has the cerrect output that Email Save

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

Step: 3

blur-text-image

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2017 Skopje Macedonia September 18 22 2017 Proceedings Part 3 Lnai 10536

Authors: Yasemin Altun ,Kamalika Das ,Taneli Mielikainen ,Donato Malerba ,Jerzy Stefanowski ,Jesse Read ,Marinka Zitnik ,Michelangelo Ceci ,Saso Dzeroski

1st Edition

3319712721, 978-3319712727

More Books

Students also viewed these Databases questions

Question

Guidelines for Informative Speeches?

Answered: 1 week ago