Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7882573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:19:04+00:00 2026-06-03T04:19:04+00:00

I have a C# project that allows users to create filters on data using

  • 0

I have a C# project that allows users to create filters on data using Regular Expressions. They can add as many filters as they want. Each filter consists of a Field and a regular expression that the user types in.

Right now it works with all AND logic. I loop through each filter and if it doesn’t match I set skip = true and break out of the loop. Then if skip == true I skip that record and don’t include it. So each and every filter must match in order for the field to be included.

However, now they want the ability to add more complex logic rules. So for example if they have created 4 filter rules. They want to be able to specify:
1 AND 2 AND (3 OR 4)
or they may want to specify
1 OR 2 OR 3 OR 4
or they may want to specify
(1 AND 2 AND 3) OR 4
and so on…I think you get the point.

I have added a textbox where they can type in the logic that they want.

I have been racking my brain and I am stumped on how to make this work. My only conclusion is to somehow be able to create a dynamic IF statement that is based on the text they type into the textbox but I don’t know if that is even possible.

It seems like there should be an easy way to do this but I can’t figure it out. If anyone could help me, I would really appreciate it.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T04:19:05+00:00Added an answer on June 3, 2026 at 4:19 am

    Here’s a full test that works as you want it with regular expressions and AND, OR and brackets. Note that this only supports the operators AND and OR and parentheses ( and ) and expects the input to be somewhat well formed (regular expressions must not have spaces). The parsing can be improved, the idea remains the same.

    Here is the overall test:

    var input = ".* AND [0-9]+ AND abc OR (abc AND def)";
    var rpn = ParseRPN(input);  
    var test = GetExpression(new Queue<string>(rpn.Reverse())).Compile();
    test("abc");    // false
    test("abc0");   // true
    test("abcdef"); // true
    

    Here is the parsing to reverse polish notation:

    public Queue<string> ParseRPN(string input)
    {
        // improve the parsing into tokens here
        var output = new Queue<string>();
        var ops = new Stack<string>();
        input = input.Replace("(","( ").Replace(")"," )");
        var split = input.Split(' ');
    
        foreach (var token in split)
        {
            if (token == "AND" || token == "OR")
            {
                while (ops.Count > 0 && (ops.Peek() == "AND" || ops.Peek() == "OR"))
                {
                    output.Enqueue(ops.Pop());
                }
                ops.Push(token);
            }
            else if (token == "(") ops.Push(token);
            else if (token == ")")
            {
                while (ops.Count > 0 && ops.Peek() != "(")
                {
                    output.Enqueue(ops.Pop());
                }
                ops.Pop();
            }
            else output.Enqueue(token); // it's a number        
        }
    
        while (ops.Count > 0)
        {
            output.Enqueue(ops.Pop());
        }
    
        return output;
    }
    

    And the magic GetExpression:

    public Expression<Func<string,bool>> GetExpression(Queue<string> input)
    {
        var exp = input.Dequeue();
        if (exp == "AND") return GetExpression(input).And(GetExpression(input));
        else if (exp == "OR") return GetExpression(input).Or(GetExpression(input));
        else return (test => Regex.IsMatch(test, exp));
    }
    

    Note this does rely on PredicateBuilder, but the extension functions used are here in there completeness:

    public static class PredicateBuilder
    {
      public static Expression<Func<T, bool>> True<T> ()  { return f => true;  }
      public static Expression<Func<T, bool>> False<T> () { return f => false; }
    
      public static Expression<Func<T, bool>> Or<T> (this Expression<Func<T, bool>> expr1,
                                                          Expression<Func<T, bool>> expr2)
      {
        var invokedExpr = Expression.Invoke (expr2, expr1.Parameters.Cast<Expression> ());
        return Expression.Lambda<Func<T, bool>>
              (Expression.OrElse (expr1.Body, invokedExpr), expr1.Parameters);
      }
    
      public static Expression<Func<T, bool>> And<T> (this Expression<Func<T, bool>> expr1,
                                                           Expression<Func<T, bool>> expr2)
      {
        var invokedExpr = Expression.Invoke (expr2, expr1.Parameters.Cast<Expression> ());
        return Expression.Lambda<Func<T, bool>>
              (Expression.AndAlso (expr1.Body, invokedExpr), expr1.Parameters);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently created an Android application that allows users to add shopping items
We have a project that sets the DataSource of a combobox, but allows users
I have a simple C# application that allows users to specify that it should
I am working on a little home grown project that allows users to drop
I have a fairly simple Rails application that allows users to manage their clients
As a project I would like to create a website that allows a user
I'm trying to create a feature in my program that allows users to download
I have a project that is supposed to run on different (at least 2)
I have a project that uses two third party libraries, both of which make
I have a project that targets both Mac OS X 10.4 and 10.5, where

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.