http://qs321.pair.com?node_id=1231789


in reply to Re: Pattern replace in a file name
in thread Pattern replace in a file name

the problem with your suggestion is it truncates the file path after the first instance of a digit, so my output looks like

/fd/gfree/tere/frf4

/fd/gfree/tere/frf4

/fd/gfree/tere/frf4

Whereas I wanted the output to be

/fd/gfree/tere/frf4545/geerg/fds/0.1

/fd/gfree/tere/frf4545/geerg/dfds/5.9

/fd/gfree/tere/frf4545/geerg/dsad/02.44

Replies are listed 'Best First'.
Re^3: Pattern replace in a file name
by hippo (Bishop) on Mar 28, 2019 at 09:47 UTC

    Now that your expected output looks more logical, a simple test can illustrate one possible solution.

    use strict; use warnings; use Test::More; my @data = ( { have => '/fd/gfree/tere/frf4545/geerg/fds/0.1/fsdf/dsakdsa/', want => '/fd/gfree/tere/frf4545/geerg/fds/0.1', }, { have => '/fd/gfree/tere/frf4545/geerg/dfds/5.9/fdsf/fdsfd/', want => '/fd/gfree/tere/frf4545/geerg/dfds/5.9', }, { have => '/fd/gfree/tere/frf4545/geerg/dsad/02.44/fdsf/fdsf/', want => '/fd/gfree/tere/frf4545/geerg/dsad/02.44', }, ); plan tests => scalar @data; for my $t (@data) { $t->{have} =~ s/(\.\d+).*?$/$1/; is $t->{have}, $t->{want}; }

      Perfect! Thanks for your time. Your solution works perfectly for me.

Re^3: Pattern replace in a file name
by hdb (Monsignor) on Mar 28, 2019 at 09:41 UTC

    If you look at the code I posted you see that I have a couple of / in it in addition to the regex I mention in the text. So the code really looks for a sequence of digits and dots between two slashes. It is true it would truncate the path after the first occurrence of such a pattern.