Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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.

Comment on Perl Regex
Download Code
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;

    Output:

    382411

    Regex:

    my ($number) = $string =~ /(\d+)$/; ^ ^ ^^ | | || | | |+ - End of the string | | + - End capture | + - One or more digits + - Begin capture
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
Re: Perl Regex
by choroba (Canon) on Nov 15, 2012 at 08:06 UTC
    TIMTOWTDI:
    #!/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: 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 <ynnor@mm.st>

      :-) 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 t.pl Number is: 382411 Number is: 382412 Number is: 382413 Number is: 382414 $ cat t.pl #!/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"; }

    ...roboticus

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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2015-07-05 08:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (61 votes), past polls