Urs Kröll
Urs Kröll

Reputation: 11

Analyze a string for boolean operator

I'm trying to make a search function for one of my programs, my plan is to let users search content with something like this

((network || system) && (ip || dns || bios)) && (version || name)

so whenever a string matches this search term like

NETWORK: ****** DNS: ******* NAME: *******

or

SYSTEM BIOS VERSION: ****

it will recognize it.

But i can't figure out how to implement this.

I thought about working through the whole search term and splitting it up to it's pieces but since i don't know how many AND or OR's are going to be in there i don't know how to do it.

Upvotes: 1

Views: 93

Answers (2)

DrKoch
DrKoch

Reputation: 9782

You can do two things:

  1. Restrict yourself to simple cases like "abc && def" then you may use simple string manipulations
  2. Complex expression (like in your question), then you need a parser.

Is is not too hard to write a simple parser yourself (if you know enough about the theory behind it). Else use a ready made toolkit like @Patrick suggested (ANTLR) which involves quite some learning curve also.

Upvotes: 1

Patrick Hofman
Patrick Hofman

Reputation: 157108

In order to do this, you need more than just some string comparison. Actually you are looking for a parser that can parse statements and form something understandable out of it, whether that is for you.

I would recommend to take a look at ANTLR, which is a parser/lexer. It has templates for C# too.

Upvotes: 3

Related Questions