1

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?

Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
  • 1
    Avoid using regex for parsing HTML. See http://www.codinghorror.com/blog/2009/11/parsing-html-the-cthulhu-way.html Try using a specialized library instead (for example, HtmlAgilityPack) – Cristian Lupascu Mar 29 '12 at 15:13
  • [Have you tried using an XML parser instead?](http://stackoverflow.com/a/1732454/26226) – jrummell Mar 29 '12 at 15:16
  • now i don't have enough time to restart but i ll keep this in my mind for the next time. thank you :) – burakokumus Mar 30 '12 at 08:15

2 Answers2

0

You can use the Any() operator from LINQ to Objects. This requires the System.Linq library (part of .NET 4.0):

if (mc[0].Groups[group].Captures.Any())
{
   ...
}

This will tell you whether the Captures collection contains any elements. If it doesn't, then of course you can't take element [0]...

Roy Dictus
  • 32,551
  • 8
  • 60
  • 76
  • I use .net framework 4. I added System.Linq but I couldn't get the Any() method work. I solved the problem with Olivier Jacot-Descombes's method. But i would be more appriciated if you have any guess about why Any() method did t work for me. I would need in the future. Thank a lot for helping :) – burakokumus Mar 30 '12 at 08:48
  • 1
    Any() is more optimal than Count because it stops processing as soon as it has determined that there are data items (it doesn't count them). You can find more information about it at http://msdn.microsoft.com/en-us/library/bb534972.aspx – Roy Dictus Mar 30 '12 at 09:42
0

This should do it.

if (mc.Count > 0 &&
    mc[0].Groups.Count > group && 
    mc[0].Groups[group].Captures.Count > 0)
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188