<?php
// http://stackoverflow.com/q/33730320/5290909
$y = 'r';
$x = 'M';
$subject = 'rrrrrr rrrrr rrrrrr rrrr rrrr.';
$regex = "/\G(?:^|(?(?<!^.).)..(?:.{4})*?)\K$y/s";
echo $result;
// => MrrMrr MrrrM rrMrrr rrrM rrMr.
?>
PD9waHAKLy8gaHR0cDovL3N0YWNrb3ZlcmZsb3cuY29tL3EvMzM3MzAzMjAvNTI5MDkwOQoKCSR5ID0gJ3InOwoJJHkgPSBwcmVnX3F1b3RlKCR5LCAnLycpOwoJJHggPSAnTSc7CgkkeCA9IHByZWdfcXVvdGUoJHgsICcvJyk7Cgkkc3ViamVjdCA9ICdycnJycnIgcnJycnIgcnJycnJyIHJycnIgcnJyci4nOwoJCgkkcmVnZXggPSAiL1xHKD86XnwoPyg/PCFeLikuKS4uKD86Lns0fSkqPylcSyR5L3MiOwoJCgkkcmVzdWx0ID0gcHJlZ19yZXBsYWNlKCRyZWdleCwgJHgsICRzdWJqZWN0KTsKCQoJZWNobyAkcmVzdWx0OwoJLy8gPT4gTXJyTXJyIE1ycnJNIHJyTXJyciBycnJNIHJyTXIuCgkKPz4=