RegexCount Function

The RegexCount() function takes two (2) or three (3) arguments and count the occurrence of second argument in first argument. The optional third parameter allows to define regex options separated by pipe (|) sign.

The valid values are : Compiled , CultureInvariant , ExplicitCapture , IgnoreCase , IgnorePatternWhitespace , Multiline , RightToLeft , Singleline , None

Regex provides flexibility and speed, the pattern parameter should be a valid pattern or string or character to search. The return value of -1 means there was an error. Search by default is case sensitive.

The syntax for RegexCount function is RegexCount( String , Pattern , [ Regex Options ] ).

/* Following will output 2 */ 
SELECT RegexCount('Hello World!', 'o') ;

/* Following will output 0 as capital E is not found. */ SELECT RegexCount('Hello World!', 'E') ;
/* Following will output 1 due to IgnoreCase. */ SELECT RegexCount('Hello World!', 'E', 'IgnoreCase') ;
/* Following will output 1 only one true as word found */ SELECT RegexCount('1,sql,database,nosql,sqldb,no,sql,sqldatabase, SQLdb, true and fase SqlLang of SQL', '\\btrue\\b', 'IgnoreCase') ;
/* Following will output 3 as only word sql is matched and all other variations are ignored. */ SELECT RegexCount('1,sql,database,nosql,sqldb,no,sql,sqldatabase, SQLdb, true and fase SqlLang of SQL', '\\bsql\\b', 'IgnoreCase') ;
/* Following will output 4 because of 1, 10, 11 and 12 total count 4 */ SELECT RegexCount('This string 1 contains number 10, 11 and 12.', '\\d+') ;
/* Following will output 0 */
SELECT RegexCount('This string contain    inside   spaces.', 'I') ;

/* Following will output 5 */
SELECT RegexCount('This string contain inside spaces.', 'I', 'IgnoreCase|IgnorePatternWhitespace|CultureInvariant') ;

Regex count can be used with alpha numeric characters and can use any valid regex pattern expression.