IDLE.
import re
def test_regular_expression(regex, test_string) :
pattern = re.compile(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 = "8765.1.87654.654321 -- 123.134.32.5 [18-03-2021] [00:03:21] GET /12_exercise_it116.html HTTP/1.1 200 56810323"
line_2 = "Accepted password for pe15 on Oct 25 2021 from 197.81.14.23 port 60695 ssh2"
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 ", test_regular_expression(regex_1, line_1))
print("regex_2", regex_2, "\t ", test_regular_expression(regex_2, line_1))
print("regex_3", regex_3, "\t ", test_regular_expression(regex_3, line_1))
print("regex_4", regex_4, "\t ", test_regular_expression(regex_4, line_1))
print("regex_5", regex_5, "\t ", test_regular_expression(regex_5, line_1))
print("regex_6", regex_6, "\t ", test_regular_expression(regex_6, line_1))
print("regex_7", regex_7, "\t ", test_regular_expression(regex_7, line_2))
print("regex_8", regex_8, "\t ", test_regular_expression(regex_8, line_2))
print("regex_9", regex_9, "\t ", test_regular_expression(regex_9, line_2))
print("regex_10", regex_10,"\t ", test_regular_expression(regex_10,line_2))
| Variable | Value Returned | String |
|---|---|---|
| regex_1 | IP address | line_1 |
| regex_2 | Day number | line_1 |
| regex_3 | Month name | line_1 |
| regex_4 | Year | line_1 |
| regex_5 | Hours, minutes, seconds | line_1 |
| regex_6 | Filename with extension | line_1 |
| regex_7 | Hostname | line_2 |
| regex_8 | Month | line_2 |
| regex_9 | Year | line_2 |
| regex_10 | Port | line_2 |
123.134.32.5 18 03 2021 00:03:21 12_exercise_it116.html pe15 Oct 2021 60695
print statement in the test code
print statement for the regex variable you are
working on
123.134.32.5 18 03 2021 00:03:21 12_exercise_it116.html pe15 Oct 2021 60695
cd it117/hw/hw7
hw7.py
123.134.32.5 18 03 2021 00:03:21 12_exercise_it116.html pe15 Oct 2021 60695
Copyright © 2024 Glenn Hoffman. All rights reserved. May not be reproduced without permission.