Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2018-04-26 19:32 GMT
Find Nodes?
    Voting Booth?