Two routes should be able to do the trick here; one way to do it could be:
First route with paths[]=/api/v1/user(/\d+/.*)? should capture 1, 2, 3, 4.
Second route with methods[]=GET paths[]=/api/v1/user/\d+
Since route 2 has more matching attributes than 1 it should be evaluated first, and you may add regex_priority=10 to the second route for future proofing (the default regex_priority is 0, and a higher priority means the route’s regex will be evaluated first).
Of course, you must make sure that these routes do not conflict with any other routes you may be configuring for other services. Adjust the regexes and matching rules accordingly to your needs and help yourself with a regex matching tool such as https://regex101.com/.
Additionally, if you wish to capture the {userid} segment at evaluation time (instead of writing extra code to retrieve it yourself from the URL), you may specify your regexes with a capturing group, for example:
paths[]=/api/v1/user(/(?<userid>\d+)/.*)?
Then retrieve the user id in your plugin(s):
-- must use ngx.ctx and not kong.ctx here until PDK implements URI captured groups lookup
local userid = ngx.ctx.router_matches.uri_captures.userid
To get the code querystring parameter, use the PDK (Plugin Development Kit) as such: