Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

regular expression

by Anonymous Monk
on Feb 28, 2010 at 05:33 UTC ( #825731=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How to delete the string anything before last '/' and also the next number
~/tmp/hfj/abc/bhd/5009.xmlreal.xml
The file should be '.xmlreal.xml'

Comment on regular expression
Re: regular expression
by McDarren (Abbot) on Feb 28, 2010 at 05:51 UTC
    my $string = '~/tmp/hfj/abc/bhd/5009.xmlreal.xml'; $string =~ s/^.*\/\d+//; print "$string\n";

    Another way is to use File::Basename to extract the file name, and then remove the bits you don't want.

    use File::Basename; $string = '~/tmp/hfj/abc/bhd/5009.xmlreal.xml'; my ($filename, $directories, $suffix) = fileparse($string); $filename =~ s/^\d+//; print "$filename\n";

    Cheers,
    Darren

      s/^.*\/\d+.//g

      I don't understand the purpose of the /g regex modifier in the above regex.

        heh.. it serves no purpose whatsoever.
        I'll remove it. Not quite sure what I was thinking about there :)
Re: regular expression
by AnomalousMonk (Abbot) on Feb 28, 2010 at 05:53 UTC

    This seems to do the trick:

    >perl -wMstrict -le "my $s = '~/tmp/hfj/abc/bhd/5009.xmlreal.xml'; $s =~ s{ \A .* / \d+ }{}xms; print qq{'$s'}; " '.xmlreal.xml'
Re: regular expression
by sauoq (Abbot) on Feb 28, 2010 at 21:27 UTC
    Assuming the filename is in $_:
    s/.*\d//;
    -sauoq
    "My two cents aren't worth a dime.";
Re: regular expression
by johngg (Abbot) on Feb 28, 2010 at 23:41 UTC

    Another way using split.

    $ perl -E ' > $str = q{~/tmp/hfj/abc/bhd/5009.xmlreal.xml}; > ( $path = ( split m{/}, $str )[ -1 ] ) =~ s{^\d+}{}; > say $path;' .xmlreal.xml $

    McDarren's suggestion of File::Basename is probably the safest and most portable though.

    Cheers,

    JohnGG

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (17)
As of 2014-10-24 16:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (132 votes), past polls