相关疑难解决方法(0)

正则表达式检测用于&while循环的半冒号终止C++

在我的Python应用程序中,我需要编写一个匹配C++ forwhile循环的正则表达式,该循环使用分号(;).例如,它应匹配此:

for (int i = 0; i < 10; i++);
Run Code Online (Sandbox Code Playgroud)

......但不是这个:

for (int i = 0; i < 10; i++)
Run Code Online (Sandbox Code Playgroud)

这看起来很琐事,直到您意识到开括号和右括号之间的文本可能包含其他括号,例如:

for (int i = funcA(); i < funcB(); i++);
Run Code Online (Sandbox Code Playgroud)

我正在使用python.re模块.现在我的正则表达式看起来像这样(我已经留下了我的评论,所以你可以更容易理解):

# match any line that begins with a "for" or "while" statement:
^\s*(for|while)\s*
\(  # match the initial opening parenthesis
    # Now make a named group 'balanced' which matches a balanced substring.
    (?P<balanced>
        # A balanced substring is either something that is not …
Run Code Online (Sandbox Code Playgroud)

c++ python regex recursion parsing

35
推荐指数
3
解决办法
10万
查看次数

解析逻辑表达式的正则表达式

我正在尝试使用正则表达式来解析带括号的逻辑表达式

例如:

((weight gt 10) OR (weight lt 100)) AND (length lt 50)
Run Code Online (Sandbox Code Playgroud)

我希望它可以解析为:

Group 1: (weight gt 10) OR (weight lt 100)
Group 2: AND
Group 3: length lt 50
Run Code Online (Sandbox Code Playgroud)

如果这个顺序改变:

(length lt 50) AND ((weight gt 10) OR (weight lt 100))
Run Code Online (Sandbox Code Playgroud)

我希望它可以解析为:

Group 1: length lt 50
Group 2: AND
Group 3: (weight gt 10) OR (weight lt 100)
Run Code Online (Sandbox Code Playgroud)

我试过的成本最高的是这个表达式:

(\((?>[^()]+|(?1))*\))
Run Code Online (Sandbox Code Playgroud)

问题在于它仅部分起作用:

((weight gt 10) OR (weight lt 100)) AND (length lt 50)

Group 1: ((weight gt 10) …
Run Code Online (Sandbox Code Playgroud)

regex

2
推荐指数
2
解决办法
165
查看次数

标签 统计

regex ×2

c++ ×1

parsing ×1

python ×1

recursion ×1