Reputation: 1899
I'm trying to split a string in C# to make it a logic for my code. The string is:
if ( x111 > 0 )
then (( x111/ x222) >= 34 and ( x222 / x222) <= 4500)
else ( if ( x333 equals 0)
then true else false)
I'm using the following code for other strings and it works fine:
string query = "if ( x111 > 0 ) then (x222 > 0 ) else true";
string result = query.Split(new string [] {"if", "then", "else" },StringSplitOptions.RemoveEmptyEntries);
Output:
( x111 > 0 )
(x222 > 0 )
true
For my above string, there is an issue that then condition contains another nested logic and I need to split it to make a logic. Can I split it based on "(" and ")" so that I'll be able to store the results for various expressions like if, then, else and nested conditions.
Upvotes: 0
Views: 187
Reputation: 156918
You are trying to build a parser using string operations... That is a bad idea and will probably never work.
I suggest you to use ANTLR, a tool built to create grammars and parser/lexers. You should give it a try. There are many examples of languages, including C#, which you can use as a start for your own language parser.
Upvotes: 4