1

my program is intended to search for images url inside a string according to specific keywords. it actually works fine, only problem is the "search not found" error. for some reason its like the code doesn't get to this "if" and wont return any error if there is no match found (last if).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Text.RegularExpressions;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            using (WebClient client = new WebClient()) 
            {
                int count = 0;
                Regex SearchItem = new Regex("http://.+?\\.jpg");
                string SearchValue = "fgdfgdf";
                string htmlCode = "fsdflkjsdfkjsdfkjdsflkhttp://www.dssdtanya.jpgfsdf;ldsmfs;dlfms;dmfs";
                Match matches = SearchItem.Match(htmlCode);

                while (matches.Success) 
                {
                    string test = matches.ToString();
                    if (test.Contains(SearchValue))
                    {
                        count++;
                        Console.WriteLine("Result #{0}: '{1}' found in the source code at position {2}.",count, matches.Value, matches.Index);
                        matches = matches.NextMatch();
                    }
                }

                Console.WriteLine(count);
                if (count == 0) { Console.WriteLine("search not found."); }
                Console.ReadKey();
            }
        }
    }
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Felix Kreuk
  • 193
  • 1
  • 9
  • This may help you http://stackoverflow.com/questions/6172748/regular-expression-for-image-url – Vivekh Oct 04 '13 at 15:27

1 Answers1

0

Your program enters an infinite loop if the first test doesn't contain the search value. Change your code to this:

while (matches.Success)
{
    string test = matches.ToString();
    if (test.Contains(SearchValue))
    {
        count++;
        Console.WriteLine("Result #{0}: '{1}' found in the source code at position {2}.", count, matches.Value, matches.Index);
    }
    matches = matches.NextMatch(); //moved this outside the if
}
EkoostikMartin
  • 6,831
  • 2
  • 33
  • 62