I have a working C# (version 5) function that I use to match an input string to one of many unique regular expression patterns and return the replacement string associated with the matched pattern (via Regex.Replace
). I've tested it well enough to know that the code acts as intended and is reliable.
One benefit of this approach is that it is readable (to me) and easy to edit any of the constant string variables. However, it is what I would consider to be "the long way."
Am I missing out on a more elegant technique that doesn't require the newing up of the seven Regex variables and a long if-else if-else block? If I need to add more patterns, then I'd be newing up more variables and adding to the if-else if-else block. I have not yet seen any better techniques in Pluralsight or in Stack Overflow (those were more concerned with fixing a specific expression and bug hunting).
The code block you see below was created in Visual Studio 2013 Ultimate as a SQL Database Project and published to a SQL Server Database (version 2014), so that the User Defined Function can be used in T-SQL Select queries. All this because T-SQL does not have true regular expression functionality as exists in C# NET.
Most of the space of this function is taken up by variable definition.
- Two strings (
pattern
andreplacement
) -- at first empty, to be assigned at the end of the function. - Seven Regular Expression patterns (the
search
input must match to one-and-only-one pattern). - Seven replacement strings, where each
replacement
string is paired to apattern
. - Seven Regex variables
The behavior takes place in a long if-else if-else block. If the search
matches any of the patterns, then the pattern
and replacement
variables are assigned.
Finally, the replacement string is returned (via the Regex.Replace(search, pattern, replacement)
function).
Are there any better approaches?
using System.Data.SqlTypes;
using System.Text.RegularExpressions;
namespace CustomClrFunctions
{
/// <summary>
/// This set of CLR functions is published to the CustomClrFunctions Database to apply
/// true Regex match and replacement functionality, as T-SQL does not (yet) provide
/// that feature.
/// </summary>
public partial class UserDefinedFunctions
{
/// <summary>
/// This function replaces the OW format into ADE format for ELA standards.
/// </summary>
/// <param name="search">An ELA standard in OW format. Example: "LA.11-12.11-12.L.1.a"</param>
/// <returns>The same ELA standard translated to ADE format.</returns>
[Microsoft.SqlServer.Server.SqlFunction]
public static SqlString RegexReplaceElaHs(SqlChars search)
{
/* Known patterns and replacements (to replace the search term) */
const string pattern1 = @"LA\.11-12\.11-12\.(\w{1,4}).(\d{1,2})(\.\w)?";
const string replacement1 = "LA.11-12.$1.$2$3";
const string pattern2 = @"LA\.9-10\.9-10\.(\w{1,4}).(\d{1,2})(\.\w)?";
const string replacement2 = "LA.9-10.$1.$2$3";
const string pattern3 = @"LA\.K-12\.CCSS\.ELA-Literacy\.CCRA\.(\w{1,2})\.(\d{1,2})";
const string replacement3 = "CCRA.$1.$2";
const string pattern4 = @"LA\.11-12\.(\d{1,2})\.(\w{1,2})\.(\d{1,2})";
const string replacement4 = "LA.$1.$2.$3";
const string pattern5 = @"LA\.9-10\.(\d{1,2})\.(\w{1,2})\.(\d{1,2})";
const string replacement5= "LA.$1.$2.$3";
const string pattern6 = @"LA\.6-8\.6-8\.(\w{1,4}).(\d{1,2})(\.\w)?";
const string replacement6 = "LA.6-8.$1.$2$3";
const string pattern7 = @"LA\.8\.8\.(\w{1,2}).(\d)(\.\w)?";
const string replacement7 = "LA.8.$1.$2$3";
var regex1 = new Regex(pattern1);
var regex2 = new Regex(pattern2);
var regex3 = new Regex(pattern3);
var regex4 = new Regex(pattern4);
var regex5 = new Regex(pattern5);
var regex6 = new Regex(pattern6);
var regex7 = new Regex(pattern7);
string pattern;
string replacement;
/* The following if-else block assigns
* values to "pattern" and "replacement"
* depending on which pattern matches "search"
*/
if (regex1.IsMatch(new string(search.Value)))
{
pattern = pattern1;
replacement = replacement1;
}
else if (regex2.IsMatch(new string(search.Value)))
{
pattern = pattern2;
replacement = replacement2;
}
else if (regex3.IsMatch(new string(search.Value)))
{
pattern = pattern3;
replacement = replacement3;
}
else if (regex4.IsMatch(new string(search.Value)))
{
pattern = pattern4;
replacement = replacement4;
}
else if (regex5.IsMatch(new string(search.Value)))
{
pattern = pattern5;
replacement = replacement5;
}
else if (regex6.IsMatch(new string(search.Value)))
{
pattern = pattern6;
replacement = replacement6;
}
else if (regex7.IsMatch(new string(search.Value)))
{
pattern = pattern7;
replacement = replacement7;
}
else
{
pattern = string.Empty;
replacement = string.Empty;
}
// This returns the transformation of the "search" value in ADE format.
// replacement is a string replacement.
return Regex.Replace(new string(search.Value), pattern, replacement);
}
}
}