Setting a Required String Length for Regex in Searching Images

Jun 26 2009 10:24 PM
Okay, here is a fun one for the regex masters.  I am searching a webpage for all images beginning with "HTTP://" and ending with ".JPG"  The problem, however, is that my current regex takes an Http:// from the first image and the .JPG from the very last image on the page with about 20 other images smashed between.  I think the only way I can fix this and return each array element as a single image path would be to limit to match size to around 50? Any other solutions?

Here is my current regex code:

  static string[] getImages(string source) 
{
//get all the images
MatchCollection mr;
String[] thumbs = new String[20];
// Create a new Regex object and define the regular expression.
// begginning with http and ending with JPG ignoring case
Regex a = new Regex("http://([^<]+).JPG", RegexOptions.IgnoreCase);
// Use the Matches method to find all matches in the input string.
mr = a.Matches(source);
// Loop through the match collection to retrieve all
// matches and positions.
for (int i = 0; i < mr.Count; i++)
{
// Add the match string to the string array.
thumbs[i] = mr[i].Value;
}
//return images in string array
return images;
}


Thanks in advance for any advice.

Regards,

William


For Clarification,

String searched =

Http://website.com/thisisanimage.jpg , Http://website.com/thisisanimage1.jpg , Http://website.com/thisisanimage2.jpg ,

This is what my Regex is returning:

match(1) = Http://website.com/thisisanimage.jpg , Http://website.com/thisisanimage1.jpg
match(2) = Http://website.com/thisisanimage2.jpg

I want it to return:

match(1) = Http://website.com/thisisanimage.jpg
match(2) = Http://website.com/thisisanimage1.jpg
match(3) = Http://website.com/thisisanimage2.jpg

Thanks!