Dynamically append custom post type to end of url

Here is something I came up with to solve this exact problem.

add_action('wp_list_categories','example_wp_list_categories');

function example_wp_list_categories($output) {
    global $post;

    foreach (get_categories() as $cat) {
        if (preg_match("/\/category\/$cat->slug\//", $output)) {
            $output = str_replace('/category/' . $cat->slug . "https://wordpress.stackexchange.com/", '/category/' . $cat->slug . '/?post_type=" . $post->post_type, $output);
        }
    }
    return $output;
}

Regex isn”t a strong point of mine, so that might be improved upon.