fork download
  1. import re, random
  2.  
  3. regex= re.compile("hash|code") # что ищем
  4. text= """ttcode test hash code testhashcode test codehash""" # целевой текст
  5. endpos=0
  6. match= regex.search(text,endpos)
  7. matches=[] # массив с совпадениями
  8. strings=[] # все остальное что не совпало, при этом если два совпадения подряд, или начало (конец) строки и совпадение, то в этот массив попадает пустая строка
  9. # поэтому значения в массивах чередуются относительно целевой строки
  10. while (match):
  11. strings+= [ text[endpos:match.start()] ]
  12. endpos= match.end()
  13. matches+=[match.group(0)]
  14. match= regex.search(text,endpos)
  15. if (endpos==len(text)): strings+= [ "" ]
  16. random.shuffle(matches)
  17.  
  18. result= "" # собираем строку обратно с перемешанными данными
  19. for i in range(len(matches)):
  20. result+=strings[i]+matches[i]
  21. result+=strings[-1]
  22.  
  23. print (result)
Success #stdin #stdout 0.15s 12264KB
stdin
Standard input is empty
stdout
tthash test code code testhashcode test codehash