开发者

c# parse a string that contains conditions , key=value

I m giving a string that contains several different combination of data.

For example :

string data = "(age=20&gender=male) or (city=newyork)"

string data1 = "(job=engineer&gender=female)"

string data2 = "(foo =1 or foo = 2) & (bar =1)"

I need to parse this string and create structure out of it and i have to evaluate this to a condition of another object. eg: if the object has these properties, then do something , else skip etc.

What are the best practices to do this?

Should i use a parser such as antlr and generate tokens out of the string. etc.?

remind开发者_运维问答er : there are several combinations of how this string is created. but it s all and/or.


Something like ANTLR is probably overkill for this.

A simple implementation of the shunting-yard algorithm would probably do the trick quite nicely.


Using regular expressions may work if the example is very simple, but it will more likely lead to a code that is impossible to maintain. Using some other approach to parsing seems like a good idea.

  • I would take a look at NCalc - it is mainly focused on parsing mathematical expressions, but it seems to be quite customizable (you can specify your functions and constants), so it may work in your scenario as well.

  • If this is too complex for your purpose, you can use any "parser generator" for C#. Using ANTLR is one great option - here is an example that shows how to start writing something like your example Five minute introduction to ANTLR

  • You could also try using F#, which is a great language for this kind of problem. See for example FsLex Sample by Chris Smith, which shows a simple mathematical evaluator - processing the parsed expression in F# would be a lot easier than in C#. In F#, you could also use FParsec, which is very lightweight, but may be a bit difficult to follow if you're not used to F#.


I suggest you to have a look at regular expressions: http://www.codeproject.com/KB/dotnet/regextutorial.aspx


Antlr is a great tool, but you can probably do this with regular expressions. One of the nice things about the .NET regex engine is support for nested constructs. See

http://retkomma.wordpress.com/2007/10/30/nested-regular-expressions-explained/

and this SO post.


Seems like you might want to use Regular Expressions to do this.

Read up a little bit on Regular Expressions in .NET. Here are some good articles:

  • http://msdn.microsoft.com/en-us/library/hs600312.aspx
  • http://www.regular-expressions.info/dotnet.html

When it comes time to write/test your Regular expression i would highly recommend using RegExLib.com's regex tester.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜