Click here to Skip to main content
15,860,972 members
Articles / Desktop Programming / Windows Forms

String Search Using Boolean Expressions

Rate me:
Please Sign up or sign in to vote.
3.86/5 (3 votes)
4 Jun 2008GPL32 min read 37.6K   550   10   7
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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questiondoes not work for below expression Pin
Jaikrishan7-Sep-11 2:13
Jaikrishan7-Sep-11 2:13 
Questionhow we can evaluate expression with equality operator Pin
Member 25954882-Jul-09 1:40
Member 25954882-Jul-09 1:40 
QuestionDoes not handle space and quotes? Pin
salman12819-Jun-09 13:44
salman12819-Jun-09 13:44 
GeneralGreat job Pin
phmayer6712-Nov-08 6:25
phmayer6712-Nov-08 6:25 
QuestionError in first Expression? Pin
TODDIDPA26-Jun-08 15:54
TODDIDPA26-Jun-08 15:54 
AnswerRe: Error in first Expression? Pin
msvcyc29-Jun-08 17:24
msvcyc29-Jun-08 17:24 
GeneralWell done! Pin
Member 33840119-Jun-08 18:01
Member 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.