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!
/my/pictures/album_123
/my/pictures/album_123foo
/my/pictures/album_123/foo
/my/pictures/album_123foo/
Specifically I need only the numbers after '/my/pictures/album_'
Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
#!/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";
}
use strict;
my @paths = qw(
/my/pictures/album_123
/my/pictures/album_123foo
/my/pictures/album_123/foo
/my/pictures/album_123foo/
);
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/albu m_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.
qw(
/my/pictures/album_123
album_456/my/pictures/albu
/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.
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?
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?
if ($url =~ /album_(\d\d\d)/) {
$num = $1;
}