There is one deliverable for this assignment
This homework assignment is going to be different from the other assignments.
You will have to do very little coding for this assignment.
Instead, I will supply you with a function that will test regular expressions using regular expressions contained in the following variables:
regex_1You must copy the code below into your script
import re def test_regular_expression(regex, test_string) : pattern = re.compile(r'' + regex ) match = pattern.search(test_string) if match : try : return match.group(1) except : print('Match found but no substring returned') return '' else: print(regex, 'does not match', test_string) return '' line_1 = 'Mar xxxxx16xxxxxxx 11:58:13 xxxxxxxxxxxxxxx 65.96.149.57 port 60695 Wed' line_2 = ' 205.236.184.32 09 Feb 2014:00:03:21 +0000 12_class_notes_it117.html HTTP/1.1" 200 56810323' regex_1 = regex_2 = regex_3 = regex_4 = regex_5 = regex_6 = regex_7 = regex_8 = regex_9 = regex_10 = print('regex_1', regex_1, '\t returned ', test_regular_expression(regex_1, line_1)) print('regex_2', regex_2, '\t returned ', test_regular_expression(regex_2, line_1)) print('regex_3', regex_3, '\t returned ', test_regular_expression(regex_3, line_1)) print('regex_4', regex_4, '\t returned ', test_regular_expression(regex_4, line_1)) print('regex_5', regex_5, '\t returned ', test_regular_expression(regex_5, line_1)) print('regex_6', regex_6, '\t returned ', test_regular_expression(regex_6, line_1)) print('regex_7', regex_7, '\t returned ', test_regular_expression(regex_7, line_2)) print('regex_8', regex_8, '\t returned ', test_regular_expression(regex_8, line_2)) print('regex_9', regex_9, '\t returned ', test_regular_expression(regex_9, line_2)) print('regex_10', regex_10,'\t returned ', test_regular_expression(regex_10,line_2))
Define the variables below.
The value of each variable should return the value given below when run on the string listed.
Variable | Value Returned | String |
---|---|---|
regex_1 | Month name | line_1 |
regex_2 | Day number | line_1 |
regex_3 | Hours, minutes, seconds | line_1 |
regex_4 | IP address | line_1 |
regex_5 | Port number | line_1 |
regex_6 | Day of the week | line_1 |
regex_7 | Two digit day number | line_2 |
regex_8 | Month | line_2 |
regex_9 | Year | line_2 |
regex_10 | The filename with extension | line_2 |
You MAY NOT use ordinary characters in your regular expression values.
For example you cannot use "html" when matching the filename.
You MAY use the period, . , as an ordinary character.
Don't forget that if you want to use a meta-character as a literal, like .you must escape it with a \.
Open an a text editor and create the file hw7.py.
You can use the editor built into IDLE or a program like Sublime.
Write this program in a step-by-step fashion using the technique of incremental development.
In other words, write a bit of code, test it, make whatever changes you need to get it working, and go on to the next step.
Put a # before each print
statement
in the test code at the bottom of the file, except for the one that
prints regex_1.
For each regular expression, don't write the entire expression all at once.
Instead build it up little by little testing as you go.
When you get this regular expression working, remove the # from the next statement and repeat the procedure.
Mar 16 11:58:13 65.96.149.57 60695 Wed 09 Feb 2014 12_class_notes_it117.html
cd it117
cd hw
mkdir hw7
ls
cd it117/hw/hw7
chmod 755 hw7.py
./hw7.py
Mar 16 11:58:13 65.96.149.57 60695 Wed 09 Feb 2014 12_class_notes_it117.html
Copyright © 2020 Glenn Hoffman. All rights reserved. May not be reproduced without permission.