in reply to How to replace in one regexp

I want to get $path from $target. How I do it in one regexp?

# How to optimize?

Those are two different questions, and are likely to have different answers. I would leave it as is, but if you insist on using one regexp, I guess (haven't tried it), something like:
$path =~ s/(?:[^/]*\?.*$)|(?:[^/]+$)/;