Click here to Skip to main content
Click here to Skip to main content
Go to top

String Search Using Boolean Expressions

, 4 Jun 2008
Rate this:
Please Sign up or sign in to vote.
Search block of text using boolean based keyword expressions
boolex.GIF

Introduction

This application as the title says allows text searching based on string expressions built using boolean operators.

Background

I was unable to find any native libraries in .NET for evaluating boolean expressions. Not sure if regular expression library supports evaluation of boolean expressions.

This code matches user supplied keywords against a block of text in its simplest form. However, users can build complex keyword expressions to match using operators like AND, OR and NOT. Users can also override precedence of operators using parenthesis. Operator precedence is very similar to the precedence rules commonly found in all programming languages. NOT operator precedes AND operator which in tun precedes OR operator.

Expression evaluation logic is simple. First, the infix expression is converted to postfix followed by the actual evaluation.

Sample Text :- Hello, World!

Input infix expression :- Hello AND World!

Equivalent postfix expression :- Hello World! AND

Result :- True

The code does not use any Recursion for postfix conversion as well as for the final evaluation. Postfix conversion uses the C# stack container to arrange keywords and operators based on precedence. Expression evaluation logic simply runs through this converted postfix expression and evaluates the keyword based on operator type - binary vs unary. Again, C# stack container is used here for easier evaluation.

Methods of interest:

  1. ConvertToPostFix()
  2. EvaluateExpression()

You may override operators or nest any number of expressions using ( and ) parenthesis.

Using the Code

The application starts up with a test text message string and few expressions. You may go ahead and hit evaluate to view the equivalent postfix strings and the evaluation results. You can also customize by entering your own text and expressions.

Points of Interest

Interesting aspect of the application was the use of non-recursive expression evaluation approach. Also, C# and .NET's stacks and dictionary objects were perfect to build the solution in no time.

As always, very much interested in hearing your constructive criticisms and suggestions.

History

  • 4th June, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

Share

About the Author

msvcyc

United States United States
No Biography provided

Comments and Discussions

 
Questiondoes not work for below expression PinmemberJaikrishan7-Sep-11 2:13 
Questionhow we can evaluate expression with equality operator PinmemberMember 25954882-Jul-09 1:40 
QuestionDoes not handle space and quotes? Pinmembersalman12819-Jun-09 13:44 
GeneralGreat job Pinmemberorkin12-Nov-08 6:25 
QuestionError in first Expression? PinmemberTODDIDPA26-Jun-08 15:54 
AnswerRe: Error in first Expression? Pinmembermsvenc29-Jun-08 17:24 
GeneralWell done! PinmemberMember 33840119-Jun-08 18:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 4 Jun 2008
Article Copyright 2008 by msvcyc
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid