?
Solved

Perl regex - Get certain numbers out of a path

Posted on 2007-10-03
6
Medium Priority
?
400 Views
Last Modified: 2008-01-09
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!
0
Comment
Question by:zumpoof
6 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 20010657
$url='/my/pictures/album_123foo/foo123';

if ($url =~ /album_(\d\d\d)/) {
   $num = $1;
}
0
 
LVL 85

Accepted Solution

by:
ozo earned 2000 total points
ID: 20011104
for( qw(
/my/pictures/album_123

/my/pictures/album_123foo

/my/pictures/album_123/foo

/my/pictures/album_123foo/foo123
) ){
   ($digits) = m#/my/pictures/album_(\d+)#;
  print "digits=$digits\n";
}
0
 
LVL 17

Expert Comment

by:mjcoyne
ID: 20011350
#!/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";
}
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 85

Expert Comment

by:ozo
ID: 20011871
The choice of just one of the 3 comments as the accepted solution seems a little arbitrary
0
 
LVL 85

Expert Comment

by:ozo
ID: 20011971
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.
0
 

Author Comment

by:zumpoof
ID: 20012071
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?
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question