import re
text = "XYZXYZXYXX XYZXYZ\nXYZXY XYZXYZ"
for line in text.splitlines():
	matches = re.findall(r'(?:XYZ)+(?:X(?:YZ?))?', line)
	if matches:
		longest = max(matches, key=len)
		print(f'{line}: {longest} (Длина: {len(longest)})')
	else:
		print(f'{line}: NO MATCH')