my $xpath = 'BoringNode[1]/InterestingNode[@InterestingAttribute="outgrabe"]/AnotherBoringNode[5]'; $xpath =~ s|\/(.*?)$||; print $xpath . "\n"; #### BoringNode[1]/InterestingNode[@InterestingAttribute="outgrabe"]