开发者

Locating multiple nested If statements using regular expressions

Is there a way to search for multiple nested if statements in code using a regular exp开发者_开发百科ression?

For example, an expression that would locate an instance of if statements three or more layers deep with different styles (if, if/else, if/elseif/else):

if (...) {
    <code>
    if (...) {
        <code>
        if (...)
            <code>
    } else if (...) {
        <code>
    } else {
        <code>
    }
} else {
    <code>
}


Using regexes to do source code searches is a bad idea. IMO. It is better to use some tool that parses the source code and then allows you to query the parse trees using (for example) XPath style path expressions.

The problem with regexes for source code searching is that they are generally too hard to read and write (unless you are a regex Guru), and they are prone to false positives and false negatives due to some edge case that the regex creator didn't think of. (For example, using \uxxxx characters in keywords.)

Here are some tool links:

  • http://en.wikipedia.org/wiki/List_of_tools_for_static_code_analysis#Java

(Please feel free to suggest others.)


Unless, I misread this the answer is definitively no. The reason is that if you have to keep track of the nesting level you are talking about a language subset that cannot be matched be a regular expression. Regular expressions can only recognize things that are captured in a deterministic finite automaton. To do something like this requires a stack or a counter which moves you up to a more powerful class of automata called a push-down automaton.


Try:

((if\(.+\)(\n)?.*\n|(else)?[ ]*(if\(.+\))?(\{)?(\n)*.*(\n)*(\})?){3}((if\(.+\)(\n)?.*\n|(else)?[ ]*if\(.+\)\{(\n)*.*(\n)*\})*

A bit verbose, but it looks for 3 or more statements consisting of an if statement with a condition and optional braces, or an else if statement with an optional condition and optional braces.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜