$re = '/^(.*)(\|.*)(\n(?:(?!msgstr).*\n)*)(msgstr)/m';
$str = 'translate italian ch1_669664cb:| mc
# "Hey."
msgstr "Hey."
translate italian ch1_177d3eb2:| mc
# "What are you doing?"
msgstr "What are you doing?"
translate italian ch1_25d98ea6:| m p_1a
# "I\'m in charge of the breakfast, remember?"
msgstr "I\'m in charge of the breakfast, remember?"
translate italian ch1_7ae83e04:|
# "Oh, that\'s right."
msgstr "Oh, that\'s right."';
$subst = "$1$4$3$2";
$result = preg_replace($re, $subst, $str);
echo "The result of the substitution is ".$result;
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php