I am trying to add a bracket to operands of OR operator. For example, if I have statement as below,
C>O AND C>4 OR C>0 AND C>5
I would like to format it as below
(C>O AND C>4) OR (C>0 AND C>5)
I wrote a simple code to do this as below. However, if the string has more than one OR statements the code doesn’t work properly. I was told that the regular expression could accomplish this. But I have very minimum knowledge in regular expression.
string mystring = "C>O AND C>4 OR C>0 AND C>5";
int indexFound = mystring.IndexOf("OR");
string left = mystring.Substring(0, indexFound - 1);
string right = mystring.Substring(indexFound + 2, mystring.Length - (indexFound + 2));
string output = "(" + left + ")" + "OR" + "(" + right + ")";
Any help would be appreciated.
Here is the way via regular expressions:
Usage:
UPDATE:
Works with