简体   繁体   English

正则表达式获取大括号之间的文本,包括其他大括号

[英]Regular Expression get text between braces including other braces

I have a "main"-string like:我有一个“主”字符串,如:

((Gripper|Open==true OR RIT|Turning==false) AND Robot|PosX >=3 OR (Test|Close==false OR (Gripper|Open==false AND RIT|Turning==false)))

I want to get three sub strings in the best case:我想在最好的情况下得到三个子字符串:

1: (Gripper|Open==true OR RIT|Turning==false)
2: Robot|PosX >=3
3: (Test|Close==false OR (Gripper|Open==false AND RIT|Turning==false))

But only two (the one in braces [1,3]) would be fine too, since they can be replaced in the main-string, getting the 3rd[2] as a result.但是只有两个(大括号 [1,3] 中的那个)也可以,因为它们可以在主字符串中替换,结果得到 3rd[2]。 Ideally with the help of regex.最好在正则表达式的帮助下。

All the sub strings go into a class as children so I can apply the regex for each child and get their sub strings as well.所有子字符串都作为子项进入一个类,因此我可以为每个子项应用正则表达式并获取他们的子字符串。

1: Test|Close==false
2: (Gripper|Open==false AND RIT|Turning==false)

For child number three (where the first result without the braces would be optional again.对于第三个孩子(没有大括号的第一个结果再次是可选的。

I tried something similar to Regular expression to extract text between braces and putting positions of the matches onto a stack, but not with the expected results.我尝试了类似于正则表达式的东西来提取大括号之间的文本并将匹配的位置放在堆栈上,但没有得到预期的结果。

The best regex I found so far is到目前为止我发现的最好的正则表达式是

([^()]+(?:[^()]+)+) or
([^()]+(?:)+)

(seriously, regex is powerful, but I have no idea what the above statements really do) which gives me (说真的,正则表达式很强大,但我不知道上面的语句到底做了什么)这给了我

 1. Gripper|Open == true OR RIT|Turning==false
 2. AND Robot|PosX >=3 OR
 3. Test|Close==false OR
 4. Gripper|Open==false AND RIT|Turning==false

But still, 3+4 should be in only one group as但是,3+4 应该只在一组中,因为

 Test|Close==false OR (Gripper|Open==false AND RIT|Turning==false)

Does anyone know how to achieve this?有谁知道如何实现这一目标?

You may try with that:你可以试试:

(?<=\))(?!\()[^()]+|\((?!\()[^)]+\)

Regex101正则表达式101

Explanation:解释:

(?<=\))(?!\()[^()]+ OR \((?!\()[^)]+\)

The first part before 'OR' basically matches AND Robot|PosX >=3 OR 'OR' 之前的第一部分基本上匹配AND Robot|PosX >=3 OR

  1. (?<=\\)) negative lookbehind: match current character if the previous character is not ) (?<=\\))负向后视:如果前一个字符不是,则匹配当前字符)
  2. (?!\\() negative lookahead : match current character if the next charcter is not ( or ) (?!\\()否定前瞻:如果下一个字符不是 ( 或 ),则匹配当前字符
  3. [^()]+ matches anything that is Neither ( nor ). [^()]+匹配任何既不是 ( 也不是 ) 的东西。

The last part after OR matches anything that starts with ( and ends with ) while ignoring any opening braces inside it. OR 之后的最后一部分匹配以()开头的任何内容,同时忽略其中的任何左大括号。

It seems like you are looking for balanced parenthesis where the matches start with 2 words divided by a pipe and then an operator followed by an equals sign似乎您正在寻找平衡括号,其中匹配以 2 个单词开头,以管道分隔,然后是运算符,后跟等号

In C# you might match either the balanced parenthesis or match a pattern that does not contain them using an alternation.在 C# 中,您可以匹配平衡括号或使用交替匹配不包含它们的模式。

(?:\(\w+\|\w+\s*[<>!=]{1,2}[^()]*(?>[^()]+|(?<o>)\(|(?<-o>)\))*(?(o)(?!)|)\)|\w+\|\w+\s*[<>!=]{1,2}\S+)
  • (?: Non capture group (?:非捕获组
    • \\(\\w+\\|\\w+\\s* Match ( then 2 words divided by a pipe and 0+ whitespace chars \\(\\w+\\|\\w+\\s*匹配(然后 2 个单词除以管道和 0+ 空格字符
    • [<>!=]{1,2}[^()]* Match any of the operators and match any char except () [<>!=]{1,2}[^()]*匹配任何运算符并匹配除()之外的任何字符
    • (?> Atomic group (?>原子团
      • [^()]+ Match 1+ times any char except () [^()]+匹配 1+ 次除()之外的任何字符
      • | Or或者
      • (?<o>)\\( Add to stack (?<o>)\\(添加到堆栈
      • | Or或者
      • (?<-o>)\\) Remove from stack (?<-o>)\\)从堆栈中移除
    • )* Close atomic group and repeat 0+ times )*关闭原子组并重复 0+ 次
    • (?(o)(?!)|)\\) Conditional with capturing group, evaluate the final subpattern (?(o)(?!)|)\\)以捕获组为条件,评估最终的子模式
    • | Or或者
    • \\w+\\|\\w+\\s*[<>!=]{1,2}\\S+ Match 2 words divided by a pipe and match operators \\w+\\|\\w+\\s*[<>!=]{1,2}\\S+匹配2个被竖线分隔的单词并匹配运算符
  • ) Close non capture group )关闭非捕获组

Regex demo 正则表达式演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM