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)"