Click here to Skip to main content
12,954,631 members (59,807 online)
Click here to Skip to main content
Add your own
alternative version


10 bookmarked
Posted 4 Jun 2008

String Search Using Boolean Expressions

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


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


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.


  • 4th June, 2008: Initial post


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


About the Author

United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

Questiondoes not work for below expression Pin
Jaikrishan7-Sep-11 2:13
memberJaikrishan7-Sep-11 2:13 
Questionhow we can evaluate expression with equality operator Pin
Member 25954882-Jul-09 1:40
memberMember 25954882-Jul-09 1:40 
QuestionDoes not handle space and quotes? Pin
salman12819-Jun-09 13:44
membersalman12819-Jun-09 13:44 
GeneralGreat job Pin
orkin12-Nov-08 6:25
memberorkin12-Nov-08 6:25 
QuestionError in first Expression? Pin
TODDIDPA26-Jun-08 15:54
memberTODDIDPA26-Jun-08 15:54 
AnswerRe: Error in first Expression? Pin
msvenc29-Jun-08 17:24
membermsvenc29-Jun-08 17:24 
GeneralWell done! Pin
Member 33840119-Jun-08 18:01
memberMember 33840119-Jun-08 18:01 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170525.1 | Last Updated 4 Jun 2008
Article Copyright 2008 by msvcyc
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid