正则表达式检测用于&while循环的半冒号终止C ++
在我的Python应用程序中,我需要编写一个匹配C ++ for
或while
循环的正则表达式,该循环使用分号(;
)。例如,它应匹配此:
for (int i = 0; i < 10; i++);
......但不是这个:
for (int i = 0; i < 10; i++)
这看起来很琐事,直到您意识到开括号和右括号之间的文本可能包含其他括号,例如:
for (int i = funcA(); i < funcB(); i++);
我正在使用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 a parenthesis: [^()] | # …or a parenthesised string: \( # A parenthesised string begins with an opening parenthesis (?P=balanced)* # …followed by a sequence of balanced substrings \) # …and ends with a closing parenthesis )* # Look for a sequence of balanced substrings\) # Finally, the outer closing parenthesis.# must end with a semi-colon to match:\s*;\s*
这适用于所有上述情况,但只要你尝试使for循环的第三部分包含一个函数就会中断,如下所示:
for (int i = 0; i < 10; doSomethingTo(i));
我认为它会中断,因为只要在开括号和右括号之间放置一些文本,“平衡”组就会匹配包含文本,因此该(?P=balanced)
部分不再起作用,因为它不匹配(由于事实括号内的文字是不同的)。
慕森王
守候你守候我
牧羊人nacy