I have been trying to implement a regex test for SQL query and would like to create optional parts where if any of these were nulls. I asked someone and mentioned that it is possible to have like nullable ‘?’ in the regex but it seems to fail everytime.
Here is my test:
[TestMethod]
public void RegexTest()
{
Regex regex = new Regex("SELECT (?<select>.*) from (?<tablename>.*) ((?<jointypecondition>INNER JOIN|LEFT JOIN|RIGHT JOIN|FULL JOIN))? ((?<joincondition>.*))? (WHERE (?<wherecondition>.*))? (ORDER BY (?<ordercondition>.*))?");
var RegexMatches = regex.Match(ToSearch);
Assert.IsTrue(regex.IsMatch(ToSearch));
if (RegexMatches.Success)
{
System.Diagnostics.Debug.WriteLine($"Regex Result Columns: {RegexMatches.Groups[1].Value}");
System.Diagnostics.Debug.WriteLine($"Regex Result Tablename: {RegexMatches.Groups[2].Value}");
System.Diagnostics.Debug.WriteLine($"Regex Result JoinTypeCondition: {RegexMatches.Groups[3].Value}");
System.Diagnostics.Debug.WriteLine($"Regex Result JoinCondition: {RegexMatches.Groups[4].Value}");
System.Diagnostics.Debug.WriteLine($"Regex Result WhereCondition: {RegexMatches.Groups[5].Value}");
System.Diagnostics.Debug.WriteLine($"Regex Result OrderCondition: {RegexMatches.Groups[6].Value}");
}
// Print a success message to the trace
System.Diagnostics.Debug.WriteLine("Test assertions passed successfully.");
}
ToSearch => SQL query variable.
Failed Part:
Assert.IsTrue(regex.IsMatch(ToSearch));
What am I doing wrong here