Donate

Regular Expression Match Capture Index Property Returns 0 Instead Of -1 In C#

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
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:
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

Comments

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

TypeScript Error Or Bug: The term 'tsc' is not recognized as the name of a cmdlet, function, script file, or operable program.

Invalid nested tag div found, expected closing tag input