JnBrymn
JnBrymn

Reputation: 25353

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

Justin '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.

Upvotes: 1

Views: 1702

Answers (2)

jjnguy
jjnguy

Reputation: 138874

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.

Upvotes: 3

user557597
user557597

Reputation:

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.

Upvotes: 0

Related Questions