<?php
echo preg_replace('/(?:(?=(bar)))?/', '[[$1]]', "foobar");

?>