Slow RegEx Problems

Joined
Mar 7, 2007
Messages
1
Reaction score
0
Hi, i'm trying to get a remote file and then do some regular expressions on the content. So far i have the following:

Code:
lstOutput.Items.Add("Checking http://thecaptainsdeck.net/index.php?action=arcade;sa=hightscore;game=1");
string pageContent = this.GetContent("http://thecaptainsdeck.net/index.php?action=arcade;sa=hightscore;game=1");
lstOutput.Items.Add("Check 1");

Match match = Regex.Match(pageContent, "<a(.*?)href=\"(.*?)\"(.*?)>(.*?)(Contact|Link|Submit Site|Add Site|Submit Your Site|Add Your Site)(.*?)</a>", RegexOptions.IgnoreCase);

lstOutput.Items.Add("Matching Results: " + match.Index.ToString());

The GetContent function simply gets the file from the url passed in:

Code:
private string GetContent(string url)
{
    string result = "Error communicating with server";

    // Check if the page has already been checked
    HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create(url);
    
    wreq.Method = "GET";
    wreq.Timeout = 5000; // 5 seconds or 5,000 milliseconds

    try
    {
        HttpWebResponse wr = (HttpWebResponse)wreq.GetResponse();

        if (wr.StatusCode == HttpStatusCode.OK)
        {
            StreamReader sr = new StreamReader(wr.GetResponseStream());
            result = sr.ReadToEnd();
            sr.Close();
        }
    }
    catch (WebException ex)
    {
        if (ex.Status == WebExceptionStatus.Timeout)
        {
            result = "The request has timed out!";
        }
        else
        {
            result = "There was some exception: " + ex.Message;
        }
    }

    return result;
}

The problem i have is that the application hangs after displaying Check 1 and the matching results line never gets displayed. From this i assume the contents of the file is retrieved successfully but the regular expression is slow. Appreciate if someone could tell me how i can modify my regular expression to make it more efficent or how i could add a timeout of the regular expression.

Cheers. Lee
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,102
Messages
2,570,645
Members
47,244
Latest member
OdessaH562

Latest Threads

Top