RegexReplace Function

The RegexReplace() function takes two (3) or four (4) or six (6) arguments and replaces the first argument string with the third argument based on second argument pattern. The fourth or sixth optional parameters allows to define regex options seperated by pipe (|) sign. The valid values are : Compiled , CultureInvariant , ExplicitCapture , IgnoreCase , IgnorePatternWhitespace , Multiline , RightToLeft , Singleline , None

The return value of -1 indicates there was an error. The count argument restricts to how many times replace should execute, default is minus one -1 which replaces all instances which match pattern.

The syntax for RegexReplace function is
RegexReplace( String , Pattern , Replace String )
RegexReplace( String , Pattern , Replace String , [Regex Options ] )
RegexReplace( String , Pattern , Replace String , [ Count ] , [ Start Index ] , [Regex Options ] )

		/* Following will remove multiple spaces and output : This string contain inside spaces. */ 
SELECT RegexReplace('This string contain inside spaces.', '([ ]{2,})' , ' ') ;

/* Following will output : This string # contains number #, # and #. */
SELECT RegexReplace('This string 1 contains number 10, 11 and 12.', '\\d+' , '#') ;

/* Following will output : ThisIsAnExampleofRegexReplaceFunction. */
SELECT RegexReplace('This Is An Example of RegexReplace Function.', '\\s+' , '') ;

/* Following will output ThisIsAn Example of RegexReplace Function. */
SELECT RegexReplace('This Is An Example of RegexReplace Function.', '\\s+' , '' , 2, 0, 'IgnoreCase') ;

/* Following will output : This Is AnExample of RegexReplace Function. */
SELECT RegexReplace('This Is An Example of RegexReplace Function.', '\\s+' , '' , 1, 9, 'IgnoreCase') ;

/* Following will output : This string 1 contains number ##, 11 and 12. */
/* It replaces only one occurrence starting at character index 14 */
SELECT RegexReplace('This string 1 contains number 10, 11 and 12.', '\\d+' , '##' , 1, 14, 'IgnoreCase|CultureInvariant') ;