Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Preliminaries For this lab you will be working with regular expressions in Pytho

ID: 3720169 • Letter: P

Question

Preliminaries For this lab you will be working with regular expressions in Python. Various functions for working with regular expressions are available in the re module. Fortunately, Python makes it pretty easy to see if a string matches a particular pattern At the top of the file we must import the re module: import re Then we can use the search () function to test whether a string matches a pattern. In the example below, the regular expression has been saved in a string called pattern for convenience: phone123-456-7890' if re.search (pattern, phon else: e): print The string matches the pattern.' print ('The string does not match the pattern.') CSE 101 - Spring 2018 Lab #13 Page 1 The r that precedes the pattern string is not a typo. Rather, the r indicates that the string is a "raw" string. In a raw string, as opposed to a "normal" string, any backslash character is interpreted as simply a backslash, as opposed to defining an escape sequence like or . Make sure you use raw strings in your Python code when defining regular expressio The * and $ at the beginning and end of the regular expression indicate that the entire string must match the regular expression, and not just part of the string. Make sure you include these symbols in your regular expressions too!

Explanation / Answer

ScreenShot

--------------------------------------------------------------------------------------------------------

Program

#import for regular expression
import re
#Function to check address
def street_addresses(addresses):
    #List to return index
    list1=[]
    #pattern to check
    pattern=r'(^d+s[A-Z][A-Za-zs]*s[Street|Road|Path](sApt.s[A-Z])?)'
    #loop to get each address in list
    for x in addresses:
        #Match and append
        if re.search(pattern,x):
            list1.append(addresses.index(x))
            #return list with matched indeces
    return list1

   #driver section to check function correction
addresses=['58 Gnarled Oak Street','12 Chirping Bird Road','173 East Main Road Apt. Q','194 Sheep Street Apt. W']
print(street_addresses(addresses))
addresses=['Main Street','12 Main Drive','5 Elm','6 Elm Apt. 5','6 main Street','19 Dark Horse street']
print(street_addresses(addresses))