#!/usr/bin/perl use warnings; use strict; my $var = "/windows/d/Perl/test/3/5/23/7"; $var =~ s%\D*(\d+)/%$1-%g; $var .= ".txt"; print $var;