1

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.?

reminder : there are several combinations of how this string is created. but it s all and/or.

DarthVader
  • 52,984
  • 76
  • 209
  • 300

5 Answers5

2

Something like ANTLR is probably overkill for this.

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

LukeH
  • 263,068
  • 57
  • 365
  • 409
1

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#.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Tomas Petricek
  • 240,744
  • 19
  • 378
  • 553
0

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:

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

Abe Miessler
  • 82,532
  • 99
  • 305
  • 486
0

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

0

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.

Community
  • 1
  • 1
3Dave
  • 28,657
  • 18
  • 88
  • 151