#!/bin/env perl use strict; use warnings; my $target = "/index.php?prefix=/user/"; my $path = $target; # How to optimize? $path =~ s{\?.*$}{}; $path =~ s{[^/]+$}{}; print "$path\n";