import re

words = ['pear', 'pineapple', 'raspberry', 'apricot', 'red currant',
         'nectarine', 'CompSci', 'macOS', 'shape', 'shade', 'shake',
         'shame', 'drop', 'reindeer', 'powdery', 'deep', 'door']

# Literals
print("Matching the pattern 'ea':")
rgx1 = re.compile(r'ea')
for word in words:
    result = rgx1.search(word)
    if result != None:
        print(f'\t{word}')
print()

# Anchors
print("Matching the anchor '^r':")
rgx2 = re.compile(r'^r')
for word in words:
    result = rgx2.search(word)
    if result != None:
        print(f'\t{word}')
print()
    
print("Matching the anchor 't$':")
rgx3 = re.compile(r't$')
for word in words:
    result = rgx3.search(word)
    if result != None:
        print(f'\t{word}')
print()


# Character classes
print("Matching the set '[oy]':")
rgx4 = re.compile(r'[oy]')
for word in words:
    result = rgx4.search(word)
    if result != None:
        print(f'\t{word}')
print()
    
print("Matching the range '[A-Z]':")
rgx5 = re.compile(r'[A-Z]')
for word in words:
    result = rgx5.search(word)
    if result != None:
        print(f'\t{word}')
print()

print("*Not* matching the set 'sha[^kmv]e':")
rgx6 = re.compile(r'sha[^kmv]e')
for word in words:
    result = rgx6.search(word)
    if result != None:
        print(f'\t{word}')
print()

print("Matching zero or more:")
rgx7 = re.compile(r'de*r')
for word in words:
    result = rgx7.search(word)
    if result != None:
        print(f'\t{word}')
print()

print("Matching one or more:")
rgx8 = re.compile(r'de+r')
for word in words:
    result = rgx8.search(word)
    if result != None:
        print(f'\t{word}')
print()

print("Matching zero or one:")
rgx8 = re.compile(r'de?r')
for word in words:
    result = rgx8.search(word)
    if result != None:
        print(f'\t{word}')
print()

# creating a regex from variables
print("Matching apricot or nectarine:")
f1 = 'apricot'
f2 = 'nectarine'
rgx_string = f'{f1}|{f2}'
rgx = re.compile(rgx_string)
for word in words:
    if rgx.search(word):
        print(f'\t{word}')
print()