- preg_replace: documentation ( source)
<?php
$routes = [
'/path/(\d+)/new/upload$',
'/path/selectable_ajax_placements/(\w+)/(\d+)',
'/path/anotherpath/to/(\d+)/from/([\d\-\_]+)/to/([\d\-\_]+)$',
'/path/([\d\w]+)/yayanotherpath/([\d\w]+)/list$',
];
foreach ($routes as $route) {
echo preg_replace('#/(.*?[!a-z0-9_\-].*?)(?=/)#i', '<regex>', $route), "\n";
}