$re = '/^(.*\\\\(?=.*\\\\))(.*-\d+\.)(.*\\\\?.*)(\n.*\2.*)*/m';
$str = 'D:\\folder1\\folder2\\folder3\\Artifact\\Artifact-1.1\\data.xxx
D:\\folder1\\folder2\\folder3\\Artifact\\Artifact-1.2\\data.abc
D:\\folder1\\folder2\\folder3\\Artifact\\Artifact-1.3\\data.123
D:\\folder1\\folder2\\folder3\\Different_Artifact\\Different_Artifact-17.1\\something here
D:\\folder1\\folder2\\folder3\\Different_Artifact\\Different_Artifact-17.4\\aaa.xxx
D:\\folder1\\folder2\\folder3\\Different_Content\\Different_Content-2.0\\abc.xxx
D:\\folder1\\folder2\\folder3\\folder4\\Different_Content\\Different_Content-2.0\\qqqqqq
D:\\folder1\\folder2\\folder3\\folder4\\Different_Content\\Different_Content-2.2\\';
$subst = "$1$2$3";
$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