且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用于识别 If 语句的正则表达式

更新时间:2023-02-17 22:43:55

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

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/