Monday, November 19, 2012

Regex Match Capture Index Property Returns 0 instead of -1

Yesterday, while working on Regex Index property, I encountered a weird problem. Supposedly, string.indexOf(string variable) returns -1 value if there's no occurrence of such substring. However, using
Code:
Regex.Match(source.ToLower(), @"regex pattern").Index
returns 0 if instead of -1 if no such substring occurs. The solution was pointed out in stack overflow: Return index position of string after match found using Group.Success in which case returns a boolean value.
So, here's the tip:
Code:
  if ((Regex.Matches(source.ToLower(), @"<ol[^>]*>").Count < 1)   
  && (Regex.Matches(source.ToLower(), @"<ul[^>]*>").Count > 0))  
  {  
    if (Regex.Match(source.ToLower(), @"<ul[^>]*>", RegexOptions.IgnoreCase).Success)  
    {  
      // your if statements here.... :)      
    }    
  } 
Greg

0 comments:

Post a Comment