Hi *,
I’m learning regular expressions and I’m playing a little with them. I proposed myself an exercise in which I have a method that removes the columns aliases in a SQL Select statement. This should work like this:
- The method can remove aliases in a SQL select statement with the AS keyword: “select ColumnA AS A”
- The method can remove aliases in a SQL select statement without the AS keyword: “select ColumnB B”
- The method can remove aliases in a SQL select statement that contains “operation characters” (like the concatenation operation character): “select ‘Hello ‘ || ‘world!’ AS HelloWorld”
So far I have created two methods that only work on specific cases. The following code provides a summary of what I’ve done and about the problems that I’m facing.
static void Main(string[] args)
{
string cols1 = "ColA as AliasA, ColB AliasB , As As ASasas, Asasasas as As";
string cols2 = "'aaa' || 'bbb' AS AliasC , 'ccc' || 'ddd' AliasD";
string answer1 = RemAliases(cols1); // Works fine
string answer2 = RemAliases2(cols2); // Works fine
string answer3 = RemAliases2(cols1); // Doesn't work
string answer4 = RemAliases(cols2); // Doesn't work
}
static string RemAliases2(string inputSql)
{
string pattern1 = @"(.+)\s+AS\s+\w+";
string replacement1 = "$1";
string pattern2 = @"(.+)\s+\w+";
string replacement2 = "$1";
string result = Regex.Replace(inputSql, pattern1, replacement1, RegexOptions.IgnoreCase);
result = Regex.Replace(result, pattern2, replacement2, RegexOptions.IgnoreCase);
return result;
}
static string RemAliases(string inputSql)
{
string pattern1 = @"(\w+)\s+AS\s+\w+";
string replacement1 = "$1";
string pattern2 = @"(\w+)\s+\w+";
string replacement2 = "$1";
string result = Regex.Replace(inputSql, pattern1, replacement1, RegexOptions.IgnoreCase);
result = Regex.Replace(result, pattern2, replacement2, RegexOptions.IgnoreCase);
return result;
}
I wasn’t expecting “RemAliases()” to work fine with “cols2”, because the “\w+” doesn’t match the “|” character. Although, I was expecting “RemAliases2()” to also work fine with “cols1”. Can someone please provide me some help in order to know why “RemAliases2()” doesn’t work fine for the “cols1” case? Please, feel free to provide any other kind of suggestions about the way I’ve used these regular expressions.
Thanks in advance.
PS: I’m using .NET 2.0