Regex for checking logical && || statements in a line

I am trying to create a regex expression to test the logical combination of strings && || and its corresponding opening and closing brackets () .

I was messing around with the hieroglyphic Regex template, but I can't get it to work correctly, mainly due to my complete lack of understanding of the Regex template.

After several hours of StackOverflow and google, this is what I still have, I feel that I'm 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 problem with what I have so far is that they are not 1 && 2 brackets that are not checked, the same with (1 && 2) && (3 || 4) . Also seems to ignore the words alltogeter (Stack && Overflow)

Examples of some lines I'm trying to check.

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

There are also a few keywords that contain marriages that can ruin things. Example:

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

Edit: As it is now, this expression works fine, checking the complexity that I need, however, the only real problem I have is just its nubers.

A sophisticated example that validates using this Regex

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

The simple line 1 && 2 does not check without parentheses, but I do not mind adding a binding to them.

all i need is to add word support instead of prime numbers, it will be a fixed list of words if that helps.

If someone can spot a mistake or point me in a better direction, it will be awesome. Thanks

Edit:

The answer to this is mellamokb . It seems the problem was that d+ should have been 0-9a-zA-Z()

Here is a template that can use it for anyone else.

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

he accurately checks what I need

Examples:

  "IsPlayer(Video) && Player(Playing)", "((IsPlayer(Video) && (Player(Playing) && ControlIsVisible(34))) || (IsPlayer(Video) && (Player(Playing) && ControlIsVisible(34)))) && ControlIsFocused(22)" 
+6
source share
1 answer

I think the reason you cannot check expressions without wrapping () is the brackets for the brackets in your main nesting logic. If you select the following parentheses, which I mark below, then the other two not wrapped expressions check:

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

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

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

So it will be:

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

Demo: http://ideone.com/jwkcpL

+1
source

All Articles