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 8585437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:03:54+00:00 2026-06-11T22:03:54+00:00

I want to validate a string in C# that contains a Boolean expression with

  • 0

I want to validate a string in C# that contains a Boolean expression with brackets.
The string should only contain numbers 1-9, round brackets, “OR” , “AND”.

Examples of good strings:

“1 AND 2”

“2 OR 4”

“4 AND (3 OR 5)”

“2”

And so on…

I am not sure if Regular Expression are flexible enough for this task.
Is there a nice short way of achieving this in C# ?

  • 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-11T22:03:55+00:00Added an answer on June 11, 2026 at 10:03 pm

    It’s probably simpler to do this with a simple parser. But you can do this with .NET regex by using balancing groups and realizing that if the brackets are removed from the string you always have a string matched by a simple expression like ^\d+(?:\s+(?:AND|OR)\s+\d+)*\z.

    So all you have to do is use balancing groups to make sure that the brackets are balanced (and are in the right place in the right form).

    Rewriting the expression above a bit:

    (?x)^
    OPENING
    \d+
    CLOSING
    (?:
        \s+(?:AND|OR)\s+
        OPENING
        \d+
        CLOSING
    )*
    BALANCED
    \z
    

    ((?x) makes the regex engine ignore all whitespace and comments in the pattern, so it can be made more readable.)

    Where OPENING matches any number (0 included) of opening brackets:

    \s* (?: (?<open> \( ) \s* )*
    

    CLOSING matches any number of closing brackets also making sure that the balancing group is balanced:

    \s* (?: (?<-open> \) ) \s* )*
    

    and BALANCED performs a balancing check, failing if there are more open brackets then closed:

    (?(open)(?!))
    

    Giving the expression:

    (?x)^
    \s* (?: (?<open> \( ) \s* )*
    \d+
    \s* (?: (?<-open> \) ) \s* )*
    (?:
        \s+(?:AND|OR)\s+
        \s* (?: (?<open> \( ) \s* )*
        \d+
        \s* (?: (?<-open> \) ) \s* )*
    )*
    (?(open)(?!))
    \z
    

    If you do not want to allow random spaces remove every \s*.

    Example

    See demo at IdeOne. Output:

    matched: '2'
    matched: '1 AND 2'
    matched: '12 OR 234'
    matched: '(1) AND (2)'
    matched: '(((1)) AND (2))'
    matched: '1 AND 2 AND 3'
    matched: '1 AND (2 OR (3 AND 4))'
    matched: '1 AND (2 OR 3) AND 4'
    matched: ' ( 1    AND ( 2 OR  ( 3 AND    4 )  )'
    matched: '((1 AND 7) OR 6) AND ((2 AND 5) OR (3 AND 4))'
    matched: '(1)'
    matched: '(((1)))'
    failed:  '1 2'
    failed:  '1(2)'
    failed:  '(1)(2)'
    failed:  'AND'
    failed:  '1 AND'
    failed:  '(1 AND 2'
    failed:  '1 AND 2)'
    failed:  '1 (AND) 2'
    failed:  '(1 AND 2))'
    failed:  '(1) AND 2)'
    failed:  '(1)() AND (2)'
    failed:  '((1 AND 7) OR 6) AND (2 AND 5) OR (3 AND 4))'
    failed:  '((1 AND 7) OR 6) AND ((2 AND 5 OR (3 AND 4))'
    failed:  ''
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want a regular expression that validate a string that allow only numbers and
I want to validate string only if contains numerics and/or 'h' or 'H' character
i want a regular expression to validate string to have only text,operators and these
I want to validate any string that contains çÇöÖİşŞüÜğĞ chars and starting at least
I want to validate a phone number input. The phone numbers should be mix
I want to validate a form fields (text) input. It should only be a
I have an arithmetic expression string exp = ((2+3.1)/2)*4.456; I want to validate by
I have a string and I want to validate that string so that it
I use Jackson and want to check that input JSON string doesn't contain duplicated
I want to validate a form element that is allowed to contain multiple email

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.