regexp_03.py

#!/usr/bin/python3
# ===================================================================
# based on: www.youtube.com/watch?v=8GKaA1aqnBg
# ===================================================================

import re

question = "Lovely spam! Wonderful spam!"

print()
print('re.search searches a strimg')

match = re.search("spam",question)
print(match)
print(bool(match))
print(match.span())
print(match.start())
print(match.end())
print(match.string)

print()
print('re.match matches the beginning of a string')

match = re.match('spam',question)
print(match)
print(match is None)
print(bool(match))

print()
print('re.match - match 5 word characters?')
match = re.match("\w{5}",question)
print(match)
print(bool(match))

print()
print('re.fullmatch - match the complete string')
print(re.fullmatch("spam",question))
print(re.fullmatch("((\w*\s*)*!)*",question))

print()
print('re.findall - find a vowel followed by a non-vowel')
match_list = re.findall("[aeiou][^aeiou]",question)
print(match_list)

print()
print('re.finditer - find a vowel followed by a non-vowel')
match_iter = re.finditer("[aeiou][^aeiou]",question)
print(match_iter)
for m in match_iter:
    s = m.start()
    e = m.end()
    print(f'match={question[s:e]}  start={m.start()} end={m.end()}')