I am having some difficulty writing a regex expression that finds words in a text that contain ‘zz’, but not at the start and the end of the text. These are two of my many attempts:
pattern = re.compile(r'(?!(?:z){2})[a-z]*zz[a-z]*(?!(?:z){2})')
pattern = re.compile(r'\b[^z\s\d_]{2}[a-z]*zz[a-y][a-z]*(?!(?:zz))\b')
Thanks
>Solution :
Well, the direct translation would be
\b(?!zz)(?:(?!zz\b)\w)+zz(?:(?!zz\b)\w)+\b
Programmatically, you could use
text = "lorem ipsum buzz mezzo mix zztop but this is all"
words = [word
for word in text.split()
if not (word.startswith("zz") or word.endswith("zz")) and "zz" in word]
print(words)
Which yields
['mezzo']
See a demo on ideone.com.