my $path = "/abc/def/ghi"; my @segments = ($path =~ m'^(/[^/]+ (/[^/]+ (/[^/]+)? )? )'x);