views:

384

answers:

2

hi there,

I'm looking to do do two things and i am looking to do them in a beautiful way. I am working on a project that allows users to upload flickr photos by simply entering their flickr image URL. It looks like this: http://www.flickr.com/photos/xdjio/226228060/

i need to:

Now i could very easily write some string methods to do the above but i think it would be messy and love learning how to do these things in regex although not being a regex ninja i am currently unable to do the above.

Any help would be very much appreciated thanks Doug

+1  A: 

Given an input string with a URL like the one you provided, this will extract the image ID for any arbitrary user:

string input = "http://www.flickr.com/photos/xdjio/226228060/";
Match match = Regex.Match(input, "photos/[^/]+/(?<img>[0-9]+)", RegexOptions.IgnoreCase | RegexOptions.SingleLine);
if(match.Success)
{
    string imageID = match.Groups["img"].Value;
}

Breaking it down, we are searching for "photos/" followed by one or more characters that is not a '/', followed by a /, followed by one or more characters that are numbers. We also put the numbers segment into a named group called "img".

Rex M
fan-bloody-tasic ;-)thanks
Doug
A: 

hi again guys,

thought i would add to this that when using the javascript asp.net validator it doesn't support the grouping name.

the regex to use in this situation would be:

photos/[^/]+/([0-9]+)

thought someone might find this useful

Doug