用于识别 If 语句的正则表达式 [英] Regular Expression to Identify If Statements

查看:64
本文介绍了用于识别 If 语句的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个正则表达式来识别 if 语句.我遇到的唯一问题是如果在括号中包含括号的语句,它会被捕获.例如:

I'm trying to write a regular expression to identify an if statement. The only problem I'm having is getting it capture if statements that have parentheses in their parenthesis. For example:

if (condition_function(params)) {
     statements;
}

我用来捕获除这些以​​外的所有 if 语句的表达式是:

My expression to capture all if statements except these is:

 if\s*\(([^\(\)]|\s)*\)\s*{(.|\s)*?}

有人知道怎么写吗?

推荐答案

我认为这可能有效.如果有人看到我没有看到的内容,例如无法正常工作的原因,请回复.

I think this may work. If anyone sees something I don't, like a reason it won't work, please respond.

if\s*\(((?:[^\(\)]|\((?1)\))*+)\)\s*{((?:[^{}]|{(?2)})*+)}

现在应该遇到的唯一问题是 if 语句中是否有 if 语句.

The only problem this should encounter now is if there is an if statement in an if statement.

我已经在每个我能想到的可能会破坏它的有效 if 语句上对此进行了测试,它唯一不起作用的是包含带有不匹配括号的字符串的语句.

I've tested this on every valid if statement that I can think of that might break it and the only thing that it does not work on is one that contains a string with an unmatched parenthesis.

更新:我发现上面的正则表达式有错误.它不会捕获在其条件或语句部分中包含带有不匹配括号的字符串的 if 语句.像下面的例子:

Update: I found an error with the above regular expression. It does not catch if statements that contains strings with unmatched parenthesis in their condition or statement sections. Like the following example:

if (var1 == "("){
    echo "{";
}

然而,这是一个有效的 if 语句.解决办法:

However this is a valid if statement. The solution:

if\s*\(((?:(?:(?:"(?:(?:\\")|[^"])*")|(?:'(?:(?:\\')|[^'])*'))|[^\(\)]|\((?1)\))*+)\)\s*{((?:(?:(?:"(?:(?:\\")|[^"])*")|(?:'(?:(?:\\')|[^'])*'))|[^{}]|{(?2)})*+)}\s*

此正则表达式捕获所有 if 语句,甚至包含不匹配括号的字符串的语句.

This regular expression captures all if statements even ones that contain strings with unmatched parenthesis.

更新:我现在有了它,它可以捕获附加到 if 语句的 else 和 else if 语句.唯一的问题是它返回的捕获组是 if 语句中的最后一个 else 和最后一个 else if.希望我也能弄清楚如何解决这个问题.

UPDATE: I now have it so that is captures the else and else if statements that are attached to if statements. The only problem is that the capture groups it returns are the last else and the last else if in the if statement. Hopefully I'll figure out how to get around that as well.

if\s*\(((?:(?:(?:"(?:(?:\\")|[^"])*")|(?:'(?:(?:\\')|[^'])*'))|[^\(\)]|\((?1)\))*+)\)\s*{((?:(?:(?:"(?:(?:\\")|[^"])*")|(?:'(?:(?:\\')|[^'])*'))|[^{}]|{(?2)})*+)}\s*(?:(?:else\s*{((?:(?:(?:"(?:(?:\\")|[^"])*")|(?:'(?:(?:\\')|[^'])*'))|[^{}]|{(?3)})*+)}\s*)|(?:else\s*if\s*\(((?:(?:(?:"(?:(?:\\")|[^"])*")|(?:'(?:(?:\\')|[^'])*'))|[^\(\)]|\((?4)\))*+)\)\s*{((?:(?:(?:"(?:(?:\\")|[^"])*")|(?:'(?:(?:\\')|[^'])*'))|[^{}]|{(?5)})*+)}\s*))*;

如果你想测试一下,这里有一个很棒的网站:http://gskinner.com/RegExr/

If you want to test it out, here's a great website for it: http://gskinner.com/RegExr/

这篇关于用于识别 If 语句的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆