这个问题在这里已经有了答案:
Using RegEx to balance match parenthesis
(4 个回答)
7年前关闭。
我想在字符串中找到所有括号 和大括号,方括号之间和引号之间的除外。
例子 :
"{ ([Approver] is_empty) }"
匹配 {, (, ) 和 }
"{ ([Appro(ver] equals 'B}r' ) }"
仍然匹配 {, (, ) 和 }
我已经完成了匹配所有括号 和大括号的正则表达式,但我不能再进一步了
@"[\{\}\(\)]"
最佳答案
我建议只读取字符串,并使用两个 bool 值,设置为 false squareBrackets 和 false 引号,然后,开始读取字符串,如果找到 '[',则将 squareBrackets 设置为 true,如果找到 '] ',再次设置为 false,与引号相同,然后,在 squareBrackets != true 和引号 != true 时只得到 {、}、( 和 )
我希望这会有所帮助;D
bool squareBrackets = false;
bool quotes = false;
如果您喜欢并打开 [ 或 ' 并且在找到新的 ] 或 ' 之前不要再阅读更多 {、}、( 或 ),则设置为 true。
关于c# - 正则表达式匹配括号和大括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21990524/