fork download
  1. import re
  2. text = "XYZXYZXYXX XYZXYZ\nXYZXY XYZXYZ"
  3. for line in text.splitlines():
  4. matches = re.findall(r'(?:XYZ)+(?:X(?:YZ?))?', line)
  5. if matches:
  6. longest = max(matches, key=len)
  7. print(f'{line}: {longest} (Длина: {len(longest)})')
  8. else:
  9. print(f'{line}: NO MATCH')
Success #stdin #stdout 0.03s 9460KB
stdin
Standard input is empty
stdout
XYZXYZXYXX XYZXYZ: XYZXYZXY (Длина: 8)
XYZXY XYZXYZ: XYZXYZ (Длина: 6)