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

Hi Monks, This is an easy one, but I need some help. I need to strip a number from the current url (REQUEST_URI). The format is:
/folder/1234.html
How can I strip out the "1234" from the above url and put that number in a variable? Thanks.

Replies are listed 'Best First'.
Re: Strip part of url
by silent11 (Vicar) on Feb 11, 2011 at 19:41 UTC
    my $url = "http://google.com/folder/1234.html"; my @parts = split /\//, $url; (my $numbers )= $parts[-1] =~ m/(\d+)/; print $numbers; # 1234

      silent11:

      That's a bit wordy, and you're using multiple regular expressions. If you make your regular expression a little more complex, you can do it like this:

      my $url = "http://google.com/folder/1234.html"; my $numbers = $url; $numbers =~ s#^.*(\d+)[^/]*$#$1#; print $numbers;

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

      Perfect. Thanks so much!
Re: Strip part of url
by johngg (Canon) on Feb 11, 2011 at 22:30 UTC
    knoppix@Microknoppix:~$ perl -E ' > @urls = qw{ > http://google.com/folder/1234.html > http://google.com/folder/abcd.html > }; > foreach $url ( @urls ) > { > $num = $url =~ m{.*/(\d+)} ? $1 : q{No match}; > say qq{$url : $num}; > }' http://google.com/folder/1234.html : 1234 http://google.com/folder/abcd.html : No match knoppix@Microknoppix:~$

    Update: Tightened up a bit.

    knoppix@Microknoppix:~$ perl -E ' > @urls = qw{ > http://google.com/folder/1234.html > http://google.com/folder/abcd.html > http://google.com/40lder/1234.html > http://google.com/40lder/abcd.html > }; > foreach $url ( @urls ) > { > $num = $url =~ m{.*/(\d+)(?!.*/)} ? $1 : q{No match}; > say qq{$url : $num}; > }' http://google.com/folder/1234.html : 1234 http://google.com/folder/abcd.html : No match http://google.com/40lder/1234.html : 1234 http://google.com/40lder/abcd.html : No match knoppix@Microknoppix:~$

    Cheers,

    JohnGG

Re: Strip part of url
by wind (Priest) on Feb 11, 2011 at 20:17 UTC
    use File::Basename; my $url = "http://google.com/folder/1234.html"; print basename($url, '.html');

    - Miller

      What makes you think that File::Basename is the correct tool to handle URLs? It will fail at least on Mac OS.

      The correct way to handle URLs is to use the URI package.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

        I agree about the URI recommendation but that code works fine on OS X.

Re: Strip part of url
by hardikar (Initiate) on Feb 12, 2011 at 12:55 UTC
    my $url = "http://www.site.com/subdomain/a9934aa.html"; // prints all the numbers only in the filename part my @buffer = split(/\//,$url); my $buffervalue = pop(@buffer); $buffervalue =~s/[^\d]//g; print $buffervalue; // for numbers in entire url my $buffer = $url; $buffer =~s/[^\d]//g; print $buffer;