I'm trying to figure out why the regexp "[A-Za-z0-9\-]+\.jpg|jpeg|gif|png" only seems to find "gif" when I run it on the source of an html file..

For example: I have the following code

string URL = "http://www.google.ca";
            string re = @"[A-Za-z0-9\-]+\.jpg|jpeg|gif|png";

            WebClient wc = new WebClient();
            Stream data = wc.OpenRead(URL);
            StreamReader sr = new StreamReader(data);
            string s = sr.ReadToEnd();

            Match match = Regex.Match(s, re, RegexOptions.IgnoreCase);
            Console.WriteLine(match.ToString());

and all that outputs is "gif". I'm trying to get it to print the full filename of the image that it finds.

string re = @"[A-Za-z0-9\-]+\.(jpg|jpeg|gif|bmp)";
        Match match = Regex.Match(s, re, RegexOptions.IgnoreCase);
        while (match.Success)
        {
            Console.WriteLine(match.ToString());
            match=match.NextMatch();
        }

That worked, thanks. Apparantly just needed the () around the extention.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.