I am getting System.IndexOutOfRangeException everytime I try to compile my program. This is where the error is happening.

public static string findlinks2(string source) 
        {
            
                char quotes = (char)34; //sets the char variable to quotes since you can't use it in a string normally
                string searchstring = (quotes + "fmt_url_map" + quotes + ":" + quotes); //sets the searchstring to "fmt_url_map":
                string[] fmt1 = System.Text.RegularExpressions.Regex.Split(source, searchstring); //splits html into 2 arrays
                
                string fmt2 = fmt1[1].ToString();

The error is occuring when is trys to assign the variable fmt2 the contents of fmt[1].ToString();. The error is obviously because the array fmt[1] does not exist but I dont know why???

Dani AI

Generated

As pointed out, the exception happened because the code tried to read an array element that didn't exist — the split produced a single element when the delimiter/pattern was not found. later confirmed the search string was wrong, which explains the missing element. The reliable fix is to never index into a split result (or any array) without checking its length first, and to prefer targeted matching or proper parsing instead of fragile string splits.

Safe-check pattern (avoid blind indexing):

var parts = System.Text.RegularExpressions.Regex.Split(source, searchPattern);
if (parts.Length > 1)
{
    var after = parts[1];
    // process 'after' safely
}
else
{
    // handle the case where the pattern is absent
}

More robust: locate the start and extract, or use Regex.Match to capture exactly what is needed (and use Regex.Escape when matching a literal substring). Example approach using Match:

var literal = Regex.Escape(searchPattern);
var m = Regex.Match(source, literal + "(.*?)\"", RegexOptions.Singleline);
if (m.Success)
{
    var captured = m.Groups[1].Value;
    // use captured value
}

If the data being extracted is JSON embedded in HTML, avoid regex for parsing JSON — use a JSON parser (System.Text.Json or Newtonsoft.Json) to find the property directly. Additional troubleshooting tips: log or inspect the raw input around where the match is expected, confirm the exact characters (quotes, escaping, whitespace, newlines), and test the regex with RegexOptions.Singleline or explicit escapes. Finally, treat missing patterns as normal conditions (handle them), rather than letting IndexOutOfRangeException reveal the problem.

Recommended Answers

All 2 Replies

It doesn't exist because it is not finding your search string.

It doesn't exist because it is not finding your search string.

Thanks I just realised I was searching for the wrong string. Big help!!! Is there any sort of systemj where I can give you a thumbs up on DANIWEB? I only joined last week

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.