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

Hi Monks, I need to grab four numbers from a long url like: /test-text-ad-1234.html The URL gets set to a variable, but then I need to grab the number. It is always on the end just before .html. How do I do this and set it to a variable?

Replies are listed 'Best First'.
Re: Grab 4 numbers from url
by NetWallah (Canon) on May 01, 2016 at 20:38 UTC
    perl -wE 'my $url=q|http://test-text-ad-1234.html|; say $url=~/(\d{4}) +\.htm/i'

            This is not an optical illusion, it just looks like one.

      Perfect! Thank you.
Re: Grab 4 numbers from url
by Athanasius (Archbishop) on May 02, 2016 at 02:43 UTC

    Hello htmanning,

    Building on NetWallah’s answer: Since you say the URLs in question are “long,” it might be unsafe to assume that the ####.htm pattern (where # is a digit) never appears in the earlier part of a URL. In which case, play safe and anchor the match to the end:

    #! perl use strict; use warnings; my $re = qr{ ( \d{4} ) \. htm l? $ }ix; /$re/ && print "$1\n" while <DATA>; __DATA__ http://test-text-ad-1234.html http://test-abc0000.htmdef-ad-5678.HTM test-ghijk-ad-XXXX.html

    Output:

    12:39 >perl 1616_SoPW.pl 1234 5678 12:39 >

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,