Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: Pattern replace in a file name

by hdb (Monsignor)
on Mar 28, 2019 at 09:22 UTC ( #1231785=note: print w/replies, xml ) Need Help??

in reply to Pattern replace in a file name

A pattern like [.0-9]+ will detect a sequence of digits and dots, also including something like 0.000.0, probably undesirably so. For your examples it would work, full code could be like this:

use strict; use warnings; my @f = qw( /fd/gfree/tere/frf4545/geerg/fds/0.1/fsdf/dsakdsa/ /fd/gfree/tere/frf4545/geerg/dfds/5.9/fdsf/fdsfd/ /fd/gfree/tere/frf4545/geerg/dsad/02.44/fdsf/fdsf/ ); s|(/[.0-9]+)/.*|$1| for @f; print "$_\n" for @f;

Replies are listed 'Best First'.
Re^2: Pattern replace in a file name
by kaushik9918 (Sexton) on Mar 28, 2019 at 09:33 UTC

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




    Whereas I wanted the output to be




      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.

      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.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1231785]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2022-09-27 17:16 GMT
Find Nodes?
    Voting Booth?
    I prefer my indexes to start at:

    Results (122 votes). Check out past polls.