正则表达式验证逻辑&&||字符串运算符 [英] Regex to validate logical && || operators in string
问题描述
我正在尝试创建一个Regex表达式来验证逻辑&&
||
字符串组合及其相应的打开和关闭()
方括号.
I am trying to create a Regex expression to validate logical &&
||
string combonation and its corresponding opening and closing ()
brackets.
我一直在使用Regex象形文字模式,但似乎无法使其正常工作,这主要是由于我完全不了解Regex模式.
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.
经过了几个小时的StackOverflow和Google搜寻,这是我到目前为止的表现,我感到自己很亲近.
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!!");
}
}
因此,到目前为止,我遇到的主要问题是,如果没有括号,那么 1&&2
,它将与(1&& 2)&&(3 || 4)
.似乎也忽略了单词alltogeter (Stack&& Overflow)
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))"
修改:现在,此表达式可以很好地验证我所需的那种复杂性,但是,我唯一真正的问题是仅它的微不足道.
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.
使用此 Regex
"((1 && 2) && (3 || 4) || ((1 && 2) && (3 || 4)))"
简单字符串 1&&2
会在没有失败的情况下进行验证,但我不介意为这些添加最粗俗的内容.
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
mellamokb 的回答非常完美.看来麻烦是 d +
必须为 0-9a-zA-Z()
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()]+[)]*)*$";
它完全验证了我的需求
示例:
"IsPlayer(Video) && Player(Playing)",
"((IsPlayer(Video) && (Player(Playing) && ControlIsVisible(34))) || (IsPlayer(Video) && (Player(Playing) && ControlIsVisible(34)))) && ControlIsFocused(22)"
推荐答案
我认为没有包装()
就无法验证表达式的原因是核心嵌套逻辑中的包装括号.如果您删除我在下面记下的以下括号,则其他两个未包装的表达式将得到验证:
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 ^^
然后,为了允许不只是数字表达式,您需要使用对要验证的内容的更宽泛的定义替换限制性的 \ d
,例如, [0-9a-zA-Z]
:
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 ^^
因此它将变成:
...[(]*[0-9a-zA-Z]+[)]*(\s+(&&|\|\|)\s+[(]*[0-9a-zA-Z]+[)]*)*$
这篇关于正则表达式验证逻辑&&||字符串运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!