Exercises: Regexes part 1

Pick up a file with some text in it. Write a script (one for each item) that prints out every line from the file that matches the requirement. You can use the script at the end of the page as a starting point but you will have to change it!

import sys
import re

if len(sys.argv) != 2:
    print("Usage:", sys.argv[0], "FILE")

filename = sys.argv[1]
with open(filename, 'r') as fh:
    for line in fh:
        print(line, end=" ")

        match = re.search(r'REGEX1', line)
        if match:
            print("   Matching 1", match.group(0))

        match = re.search(r'REGEX2', line)
        if match:
            print("   Matching 2", match.group(0))