Ham. Abdu2 Ham. Abdu2 - 1 year ago 121
C# Question

Regex to replace BETWEEN SQL operator with AND

I have SQL Where clause to run on DataView, but C# DataView does not accept BETWEEN operator. I need to convert any BETWEEN operator in query string to And, example:

Before: Year BETWEEN 2010 and 2014

After: Year >= 2010 and Year <= 2014

I tried this pattern

$.+ BETWEEN .+ AND .+/

But it didn't work, also not replacing anything so far

Answer Source

This works, but I think you would do better to write a query that matches the required syntax...

var sql = "blah Year BETWEEN 2010 and 2014 blah";
var munged = Regex.Replace(sql,
    " $1 >= $2 and $1 <= $3 ", RegexOptions.IgnoreCase);

Note that I'm limiting the handling here to integers; expanding it to cope with strings is much more complex.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download