I found some solution:
public static class StringParser
{
public static List<string> ParseExpression(string expression)
{
//expression = System.Text.RegularExpressions.Regex.Replace(expression, @"\s+", " ");
string word = string.Empty;
int i = 0;
List<string> list = new List<string>();
while (i < expression.Length)
{
if (expression[i] == ' ')
{
if (!string.IsNullOrEmpty(word))
{
list.Add(word);
word = string.Empty;
}
i++;
continue;
}
if (expression[i] == '=')
{
if (!string.IsNullOrEmpty(word))
{
list.Add(word);
}
word = new string(expression[i], 1);
list.Add(word);
word = string.Empty;
i++;
continue;
}
if (expression[i] == '<')
{
if (!string.IsNullOrEmpty(word))
{
list.Add(word);
}
word = new string(expression[i], 1);
i++;
word += expression[i];
list.Add(word);
word = string.Empty;
i++;
continue;
}
word += expression[i];
i++;
if (!string.IsNullOrEmpty(word) && i == expression.Length)
{
list.Add(word);
}
}
return list;
}
}
Unit tests prove that it works:
[TestFixture]
public class VbExpressionParserTests
{
[Test]
public void VbExpressionParserTests_ParseExpression_String_List1()
{
string[] correctResult = { "stringVar1", "=", "\"stringVar1Value\"", "and", "boolVar1", "<>", "False", "or", "intVar1", "=", "22", "and", "intVar2", "=", "33" };
string[] res = StringParser.ParseExpression("stringVar1 = \"stringVar1Value\" and boolVar1<>False or intVar1=22 and intVar2=33").ToArray();
CollectionAssert.AreEqual(correctResult, res);
}
[Test]
public void VbExpressionParserTests_ParseExpression_String_List2()
{
string[] correctResult = { "stringVar1", "=", "\"stringVar1Value\"", "and", "boolVar1", "<>", "False", "or", "intVar1", "=", "22", "and", "intVar2", "=", "33" };
string[] res = StringParser.ParseExpression("stringVar1= \"stringVar1Value\" and boolVar1<>False or intVar1=22 and intVar2=33").ToArray();
CollectionAssert.AreEqual(correctResult, res);
}
[Test]
public void VbExpressionParserTests_ParseExpression_String_List3()
{
string[] correctResult = { "stringVar1", "=", "\"stringVar1Value\"", "and", "boolVar1", "<>", "False", "or", "intVar1", "=", "22", "and", "intVar2", "=", "33" };
string[] res = StringParser.ParseExpression("stringVar1 = \"stringVar1Value\" and boolVar1<>False or intVar1=22 and intVar2=33").ToArray();
CollectionAssert.AreEqual(correctResult, res);
}
[Test]
public void VbExpressionParserTests_ParseExpression_String_IdealExpression()
{
string[] correctResult = { "stringVar1", "=", "\"stringVar1Value\"", "and", "boolVar1", "<>", "False", "or", "intVar1", "=", "22", "and", "intVar2", "=", "33" };
string[] res = StringParser.ParseExpression("stringVar1=\"stringVar1Value\" and boolVar1<>False or intVar1=22 and intVar2=33").ToArray();
CollectionAssert.AreEqual(correctResult, res);
}
}
