You have not given any flavor of regex, so I chose my own (C#) in this solution.
A possible approach is to replace tokens in multiple loops. The C# regex allows to globally replace patterns (this covers the and/or tokens). To overcome the limit that regex cannot count, you may apply lambda expressions that count up a variable that is defined outside of that lambda expression.
E.g. a crude version of this could be coded in C# like this:
static string Translate(string input)
{
string result = input;
result = Regex.Replace(result, @"\b(and|or)\b",
m => m.Groups[1].Value.ToUpper(),
RegexOptions.CultureInvariant|RegexOptions.IgnoreCase);
int i = 0;
result = Regex.Replace(result, @"(\?)", m => "@" + (i++).ToString());
return result;
}
static void Main(string[] args)
{
string input = "Code = ? And Desc = ? Or Qty = ?";
Console.WriteLine("{0} --> {1}", input, Translate(input));
}
You may of course try to merge all into one regex but that becomes quite messy.
Cheers
Andi