$re = '/((?:\/[A-Za-z0-9-]+)+)?(\/tag\/[A-Za-z0-9-%]+)+(\/.*)?/';
$str = 'http://example.com/cat1/subcat3/subcat4/tag/this%20is%20page/asdasda?start=130/asdasdasd
http://example.com/cat1/subcat3/subcat4/tag/this%20is%20pageasdasd
example.it/news/tag/this%is%20n%page?adsadsadasd
http://example.com/tag/thispage/asdasdasd.-?asds=
http://example.com/tag/this%20is%20page/asdasd
http://example.com/tag/this
';
$subst = "$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