Click here to Skip to main content
Licence GPL3
First Posted 4 Jun 2008
Views 14,810
Downloads 166
Bookmarked 9 times

String Search Using Boolean Expressions

By | 4 Jun 2008 | Article
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)

About the Author

msvcyc



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questiondoes not work for below expression PinmemberJaikrishan2:13 7 Sep '11  
Questionhow we can evaluate expression with equality operator PinmemberMember 25954881:40 2 Jul '09  
QuestionDoes not handle space and quotes? Pinmembersalman12813:44 19 Jun '09  
GeneralGreat job Pinmemberorkin6:25 12 Nov '08  
QuestionError in first Expression? PinmemberTODDIDPA15:54 26 Jun '08  
AnswerRe: Error in first Expression? Pinmembermsvenc17:24 29 Jun '08  
GeneralWell done! PinmemberMember 338401118:01 9 Jun '08  

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.

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