Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Perl Regex

by Anonymous Monk
on Nov 15, 2012 at 05:16 UTC ( #1003931=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hiya Monks, I have a doubt regarding perl regex, Pls can you look into this,

$string = "/qwjhj/sdvbb/vuvs/tryt3687fbhvh/vv1vh/382411" $string = "/qwj23hj/sdx4vbb/vu6vs/tryvh/vv1sdfvh/382412" $string = "/qwjhj/sdvbb/vuvs/t3687fbhvh/vv1/123v1vh/382413" $string = "/qwj35hj/sd3vbb/vuf3vs/tr334tg3vh/vv1c45vh/382414"

I need to extract the last numbers(382411 or 382412 or 382413 ...) in each string. Can you pls suggest me a way to do this.

If my question is too basic, I am sorry, I am not a programmer. :), Thanks to all.

Replies are listed 'Best First'.
Re: Perl Regex
by Kenosis (Priest) on Nov 15, 2012 at 05:25 UTC

    Here's an option:

    use strict; use warnings; my $string = "/qwjhj/sdvbb/vuvs/tryt3687fbhvh/vv1vh/382411"; my ($number) = $string =~ /(\d+)$/; print $number;




    my ($number) = $string =~ /(\d+)$/; ^ ^ ^^ | | || | | |+ - End of the string | | + - End capture | + - One or more digits + - Begin capture
Re: Perl Regex
by rovf (Priest) on Nov 15, 2012 at 09:10 UTC

    use File::Basename qw(basename); .... my $last_part_in_string=basename($string);
    Depending on how you see it, you might call this an abuse, or an innovative usage of basename...

    Ronald Fischer <>

      :-) I had almost posted a similar version. There is still room for File::Spec::Unix.

Re: Perl Regex
by roboticus (Chancellor) on Nov 15, 2012 at 11:45 UTC

    There are many ways to skin a cat. Another one is:

    $ perl Number is: 382411 Number is: 382412 Number is: 382413 Number is: 382414 $ cat #!/usr/bin/perl use strict; use warnings; use Data::Dumper; for my $string ( "/qwjhj/sdvbb/vuvs/tryt3687fbhvh/vv1vh/382411", "/qwj23hj/sdx4vbb/vu6vs/tryvh/vv1sdfvh/382412", "/qwjhj/sdvbb/vuvs/t3687fbhvh/vv1/123v1vh/382413", "/qwj35hj/sd3vbb/vuf3vs/tr334tg3vh/vv1c45vh/382414" ) { my @temp = split /\//, $string; print "Number is: $temp[-1]\n"; }


    Cats are rather flighty in this neighborhood. I wonder why?

Re: Perl Regex
by choroba (Bishop) on Nov 15, 2012 at 08:06 UTC
    #!/usr/bin/perl use warnings; use strict; my @strings = ("/qwjhj/sdvbb/vuvs/tryt3687fbhvh/vv1vh/382411", "/qwj23hj/sdx4vbb/vu6vs/tryvh/vv1sdfvh/382412", "/qwjhj/sdvbb/vuvs/t3687fbhvh/vv1/123v1vh/382413", "/qwj35hj/sd3vbb/vuf3vs/tr334tg3vh/vv1c45vh/382414", ); for (@strings) { print scalar reverse ((reverse $_) =~ /([0-9]+)/), "\n"; }
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Find number at string end
by Anonymous Monk on Nov 15, 2012 at 05:58 UTC
    use v5.10.0; use strict; my $n; for my $s ( '/p/q/38948394' , 'b/62837893' , '/a/p/djo' , '/743339' ) { $s =~ m{/ (?<d>[0-9]+) $}x and $n = $+{d}, say "number: $n"; } __END__ number: 38948394 number: 62837893 number: 743339

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1003931]
Approved by 2teez
[ambrus]: "you are far more likely to be squished between two other dudes" => wait, really? aren't most planes, weighted by people traveling on it, so narrow that there are only two or three seats on each side of the one corridor, so only one third of the people ge
[ambrus]: t squished between two other people?
holli is about to take the first hit since a month and this weed is strong shit. Should my head explode, someone call 911.
[ambrus]: And even when I traveled on a big plane for a long flight, it had 3 seats, then corridor, then 4 seats, then corridor, then 3 seats, so less than half of the people sit between two other dudes.

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (11)
As of 2017-12-18 13:37 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (486 votes). Check out past polls.