Class 4 Lab 11 ‐ Regular Expressions - Justin-Boyd/Python-Class GitHub Wiki

Task 1

  • Click on File from the top menu and select Open File. Verify the displayed folder is /home/student/workspace, select main.py, and click Open.

Task 2

def main():
    pattern = "[A-Z]+[a-z]+"
    match = re.search(pattern,ex_string)
    print(match)

Task 3

    pattern  = "([A-Z]+)([a-z]+)"
    match = re.search(pattern ,ex_string)
    print(match.group())

Task 4

    print(match.expand('\\2,\\1'))

Task 5

    matches = re.findall(pattern, ex_string)
    print(matches)

Task 6

    url_pattern = "http://\S+"
    match = re.search(url_pattern ,ex_string)
    print(match.group())

Task 7

    res = re.sub('ubuntu','kali',ex_string,flags=re.I)
    print(res)

Task 8

    ip_pattern = "(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})"
    with open('/home/student/workspace/ip_logs.txt','r') as f:
        file_content = f.read()
        res = re.findall(ip_pattern,file_content)
        for ip in res:
            print(ip)

Task 9

    ip_pattern   = "(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d{1,5})"
    with open('/home/student/workspace/ip_logs.txt','r') as f:
        file_content = f.read()
        res = re.findall(ip_pattern  ,file_content)
        for ip_port in res:
            if ip_port[1]:
                print(ip_port)