pattern = r'\bcool\b'
text = "cool"
matches = re.finditer(
pattern=pattern,
string=text.strip(),
flags=re.IGNORECASE
)
if matches:
for match in matches:
print(f"match: {match.group(0): <20} start: {match.start():<5} end: {match.end():<5} pos: {match.endpos:<5}")
---
match: cool start: 0 end: 4 pos: 4
pattern = r'[\b\s]cool\b' # <-----
text = "cool"
matches = re.finditer(
pattern=pattern,
string=text.strip(),
flags=re.IGNORECASE
)
if matches:
for match in matches:
print(f"match: {match.group(0): <20} start: {match.start():<5} end: {match.end():<5} pos: {match.endpos:<5}")
---
No match