I have come up with a solution.
First I have assigned a slug to the custom post type:
$rewrite = array(
'slug' => 'something',
'with_front' => false,
'pages' => false,
'feeds' => false,
);
The I use a filter and a regex.
function 1234_filter_custom_post_url( $url, $post ) {
if ( 'my_custom_post_type' == get_post_type( $post ) ) {
$url = preg_replace('/something\/(.+)\//', '#$1', $url);
}
return $url;
}
add_filter( 'post_type_link', '1234_filter_custom_post_url', 10, 2 );
https://codex.wordpress.org/Plugin_API/Filter_Reference/post_type_link
post_type_link is a filter applied to the permalink URL for a post or
custom post type prior to being returned by the function
get_post_permalink.