开发者

Regex: match certain code that is NOT commented out

I'm looking for all code branch points in a 1000 line piece of code. That is, I want to find each occurrence of {if, elseif, for, switch, while, etc.} which is not behind a comment "%".

How?


examples

In if(x > 50) match if.

In func(x) % What do I tell my boss if this doesn't work? don't match if.


Update

Justi开发者_如何学编程n 'jjnguy' Nelson came with an answer that worked using basic regex. (Thanks Justin) However I had anticipated the solution requiring regex lookaround. Does anyone have a solution that uses lookaround? I'm having trouble getting my attempts to work.


This can be very simple, I believe, if you assume no multi-line comments, and no comment chars in String literals:

/^[^%]*(if|elseif|for|switch|while|etc)/

This will match the given keywords as long as there is no % before them.


This is my perception, it could be wrong.
You would have to use a parser specific to the language.
On the other hand, comments usually follow a strict behavior order,
that has to do with escaped/non-escaped, to end of line, line spanning, etc..

These rules can usually be applied with a regular expression because
comments don't follow language rules per se. Its not easy though, but its doable.
After they are stripped out, you can apply another regex to whats left to find
keywords.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜