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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:15:40+00:00 2026-06-18T12:15:40+00:00

I am trying to create a Regex expression to validate logical && || string

  • 0

I am trying to create a Regex expression to validate logical && || string combonation and its corresponding opening and closing () brackets.

I have been messing with the Regex hieroglyphic pattern but can’t seem to get it working correctly, mainly due to my complete lack of understanding of the Regex pattern.

After several hours of StackOverflow and google this is what I have so far, I feel I am close.

    private void ValidationTest()
    {
        string hieroglyphics = @"^(?=^[^()]*\((?>[^()]+|\((?<DEPTH>)|\)(?<-DEPTH>))*(?(DEPTH)(?!))\)[^()]*$)[(]*\d+[)]*(\s+(&&|\|\|)\s+[(]*\d+[)]*)*$";

        var tests = new List<string>
        {
            // Working
             "(1 && 2)",
             "((1 && 2) && (3 || 4))",
             "((1 && 2) && (3 || 4) || ((1 && 2) && (3 || 4)))",

            // Not working
            "(Stack && Overflow)"
        };

        if (tests.All(test => Regex.IsMatch(test, hieroglyphics)))
        {
            MessageBox.Show("Woohoo!!");
        }
    }

So the main issue with what I have so far is if ther are no brackets 1 && 2 it wont validate, same with (1 && 2) && (3 || 4).
Also it seems to ignore words alltogeter (Stack && Overflow)

Examples of some strings I am tring to validate.

"IsRecording && IsPlaying"
"IsVisible && (IsPlaying && (IsMusic || IsRadio))"

There is also some keywords that contain brakets that could mess things up
Example:

"IsWindowVisible(2) && (IsControlVisible(22) && IsControlFocused(100))"

Edit:
As it is now this expression works fine validating the kind of complexity I need, however the only real issue I have is that its nubers only.

Complex example that validates fine with this Regex

"((1 && 2) && (3 || 4) || ((1 && 2) && (3 || 4)))"

A simple string 1 && 2 wont validate without brakets, but I dont mind adding brackest to these.

all I need is to add support for words instead of just numbers, this will be a fixed list of words if that helps.

If someone can spot the error or point me in a better direction would be awesome
Thanks

Edit:

Answer by mellamokb worked perfect. It seems the trouble was the d+ needed to be 0-9a-zA-Z()

Here is the pattern incase it usefull for anyone else.

   string hieroglyphics = @"^(?=^[^()]*(?>[^()]+|\((?<DEPTH>)|\)(?<-DEPTH>))*(?(DEPTH)(?!))[^()]*$)[(]*[0-9a-zA-Z()]+[)]*(\s+(&&|\|\|)\s+[(]*[0-9a-zA-Z()]+[)]*)*$";

it validates exactly what I need

Examples:

 "IsPlayer(Video) && Player(Playing)",
 "((IsPlayer(Video) && (Player(Playing) && ControlIsVisible(34))) || (IsPlayer(Video) && (Player(Playing) && ControlIsVisible(34)))) && ControlIsFocused(22)"
  • 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-18T12:15:40+00:00Added an answer on June 18, 2026 at 12:15 pm

    I think the reason you are not able to validate expressions without a wrapping () is the wrapping parentheses in your core nesting logic. If you take out the following parentheses I note below, then the other two non-wrapped expressions validate:

    ^(?=^[^()]*\((?>[^()]+|\((?<DEPTH>)|\)(?<-DEPTH>))*(?(DEPTH)(?!))\)[^()]*$...
               ^^ remove this                            remove this ^^
    

    Then in order to allow expressions that are not just numerical, you need to replace your restrictive \d with a more liberal definition of what you want to validate, say, [0-9a-zA-Z]:

    ...[(]*\d+[)]*(\s+(&&|\|\|)\s+[(]*\d+[)]*)*$
           ^^ change these expression ^^
    

    So it would become:

    ...[(]*[0-9a-zA-Z]+[)]*(\s+(&&|\|\|)\s+[(]*[0-9a-zA-Z]+[)]*)*$
    

    Demo: http://ideone.com/jwkcpL

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a regex expression to match any string that has exactly
I'm new to regex and trying to create the expression for a string that
I'm no expert on Regex. I'm trying to create a regular expression that will
I am trying to use regex generators to create an expression, but I can't
I'm trying to create a regex to tokenize a string. An example of the
I'm trying to learn Regex & URL Rewriting in PHP. How can I create
I'm trying to create a regex that matches the inverse of a certain string
I am trying to create a RegEx expression that will successfully parse the following
I am trying to make a regular expression, that allow to create string with
I am trying to create a .NET RegEx expression that will properly balance out

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.