Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Regx Question

by DS (Acolyte)
on Jul 30, 2002 at 22:14 UTC ( [id://186378]=perlquestion: print w/replies, xml ) Need Help??

DS has asked for the wisdom of the Perl Monks concerning the following question:

Hello floks, I have this info stores in a file :
\nbs\main\den\rel_10.\cr_Q00472526-03-1\final
how can I replace the file cr_Q00472526-03-1 with nothing I need it to look like this :
\nbs\main\den\rel_10.\final
I tried to use s/
$data =~s/\w+\\$//;
I don't know why it is not doing it :(

Replies are listed 'Best First'.
Re: Regx Question
by mkmcconn (Chaplain) on Jul 30, 2002 at 22:28 UTC
    my $path = '\nbs\main\den\rel_10.\cr_Q00472526-03-1\final'; $path =~ s/[^\\]+\\(\w+)$/$1/ ; print $path; __END__ \nbs\main\den\rel_10.\final

    Update:
    My conscience nags me that this answer is not reliable. Suppose you have something at the end that doesn't match \w+, as is (all too) typical of modern file paths?

    So, here's another version that's more cautious

    my ($leaf ) = $path =~ m/[^\\]+$/g ; #save the leaf $path =~ s/(.+)\\.+\\$leaf$/$1/; #prune the branch $path .= "\\$leaf"; #graft the leaf print "$path\n"; __END__ \nbs\main\den\rel_10.\final

    Hope that helps
    mkmcconn
      Hey mkmcconn , it works fine but when the path get longer it doesn't ,, I am trying to caputure the last one before \final no matter how big is the path and delete it ,, I am trying to modify it here hopefully , I will get it .. thanks
        my @chunks = split /\\/ => $data; splice @chunks => -2, 1 if $chunks [-1] eq 'final'; $data = join "\\" => @chunks;
        Abigail
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Regx Question
by Cine (Friar) on Jul 30, 2002 at 22:18 UTC
    hmm.
    s/\\cr_Q00472526-03-1// should do it.

    T I M T O W T D I
Re: Regx Question
by Anonymous Monk on Jul 31, 2002 at 11:14 UTC
    Here's a method that doesn't use regular expressions
    #!/usr/bin/perl -w use File::Basename; fileparse_set_fstype("MSDOS"); my $path = '\nbs\main\den\rel_10.\cr_Q00472526-03-1\final'; $path = dirname(dirname($path))."\\".basename($path); print $path,"\n";
    Obviously if you are running this on DOS/Windows you probably won't need the fileparse_set_fstype.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://186378]
Approved by VSarkiss
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-19 19:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found