gpt4 book ai didi

c++ - 检测圆括号,打开和关闭

转载 作者:搜寻专家 更新时间:2023-10-30 23:58:03 26 4
gpt4 key购买 nike

我需要打破一个表达式,例如 "(a+b*9 )-10" 来提取/检测 () .

这就是我现在所拥有的,但是这两种情况都不起作用。

const regex Parser::rxOpenBracket = regex("^\\s*[(]\\s*$");

const regex Parser::rxCloseBracket = regex("^\\s*([)])\\s*$");

一些帮助会很好。谢谢!

最佳答案

或者你可以这样写:

const regex Parser::bracketPair = regex("\\(.+?\\)");

表达式翻译成这样:

\(.+?\)
\( => The opening bracket (escaped as ( would indicate a group in regex)
. => Any character
* => Zero or more times (referring to the .)
? => Lazy, stop as soon as possible (referring to the *)
\) => End bracket, also escaped

通过使用它,您将只能找到以左括号开头,后跟没有字符或任意数量的字符,直到找到右括号的匹配项。

例如 (hello world) 会被处理,但是 )hello world( 会被忽略

关于c++ - 检测圆括号,打开和关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22094572/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com