Is there any way to handle the exception if regex.Matches
didn't match anything and trying to return matches.group[]
I have a list of 10 albums in HTML divs. Each album has 5 properties like album cover path
, album link
, album name
etc.
I have done HTML parsing by using regex matchcollection in a function and I call this function for each property.
Regex regex = new Regex(pattern, RegexOptions.Compiled);
MatchCollection mc = regex.Matches(source);
string icerik = mc[0].Groups[group].Captures[0].Value;
I get a clear list like this:
- image path:
http...
- album name:
...
If these parts exist in the html there is no problem. But the problem is, what if, for example, image path of 2nd album isn't provided in html? In this case, there is no match happens and mc[0].Groups[group].Captures[0].Value
causes out of boundary exception.
What can I do to block this error before it happens?