Link to home
Start Free TrialLog in
Avatar of zumpoof
zumpoof

asked on

Perl regex - Get certain numbers out of a path

I need a regex to extract the digits out of the path "/my/pictures/album_\d\d\d".  It would need to be able to handle the following types of URLs:


/my/pictures/album_123

/my/pictures/album_123foo

/my/pictures/album_123/foo

/my/pictures/album_123foo/foo123

Specifically I need only the numbers after '/my/pictures/album_'


Thanks!
Avatar of Tintin
Tintin

$url='/my/pictures/album_123foo/foo123';

if ($url =~ /album_(\d\d\d)/) {
   $num = $1;
}
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
#!/usr/bin/perl -w
use strict;

my @paths = qw(
    /my/pictures/album_123
    /my/pictures/album_123foo
    /my/pictures/album_123/foo
    /my/pictures/album_123foo/foo123
);

for (@paths) {
    /album_(\d+)/;
    print "$1\n";
}
The choice of just one of the 3 comments as the accepted solution seems a little arbitrary
The 3 do differ in what they would do with
qw(
    /my/pictures/album_123
    album_456/my/pictures/album_123
    /my/pictures/1234
    /my/pictures/album_12
);
but since the question was somewhat ambiguous about exactly should be done in those cases
it would seem more fair to clarify the question before rejecting answers that didn't correctly guess the intent of the question.
Avatar of zumpoof

ASKER

Ozo,

My apologies; I just realized that I wasn't clear enough in the cases I listed and will post a clarification next time. Is there anything I can do to rectify the what's been done?