Link to home
Start Free TrialLog in
Avatar of KingDumbNo
KingDumbNo

asked on

Regex needed to verify coding consistency (C# in particular, but others would benefit)

I've been burnt...

I would like to show all lines of code where keywords ('for', 'while', 'if', etc.) are not followed by a '{' before another one of the keywords.  I suppose regular expression can be used for this, but a kick-start would be great.

Basically, I want to eliminate the "one-liners" problem that exists when some coders take the shortcut and skip putting the code block in {} brakets when there is only one line of code needed.

TIA, Emory.
Avatar of jhshukla
jhshukla
Flag of United States of America image

grep -e "(for|while|if|else|do).*?(for|while|if|else|do)\{" filename
SOLUTION
Avatar of jhshukla
jhshukla
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Using regexes is always cool - and dotnet regexes are perfectly good for this, but I have another suggestion. If you are working in Visual Studio, you can write an add-in that makes use of the Code Dom. In other words, let Visual studio do the parsing for you.

Here's a link:

http://www.15seconds.com/issue/020917.htm
Avatar of KingDumbNo
KingDumbNo

ASKER

DominicCronin: looks a little overwhelming, plus I can't go adding stuff like this at work.

jhshukla and ozo: I won't be able to test these for a while.  I don't have grep or perl.  What I intended to use was SourceSafe's Find In Files dialog.  There is a check box where you can choose 'regular expressions' but it turns out that this is _only_ so you can search with the one and only wildcard (*).  WOW! Stop the press!  ...Well, further puns elude me at the moment...

Thanks so far, and I will try to devise a way to use the regexes so kindly provided.

Regards,
Emory
if you can't dload grep or perl, then use Emacs' built-in searcher. Can do both literal and regex search. and you don't need to install anything, just dload the binaries for your OS and delete (or hide) the folder when you are done.
http://www.gnu.org/software/emacs/emacs.html
If my hands are tied from downloading grep and perl, it logically follows (however illogical the policy since this is for professional use) that I cannot download emacs.

Anyhow, I now have NuMega's Devpartner Professional, which has editable "Rules" for code review and fully integrated with .NET, possibly along the lines of what DominicCronin suggested.  Makes perfect sense for this tool to use regexes, but the flavor is not compatible with the following regex supplied by ozo:

(for|while|if|else|do)[^\{]*?(for|while|if|else|do).*?\{

even when I slightly changed it to this:
(for|while|if|else|do)[^\{].*?(for|while|if|else|do).*?\{
I just added a period in the middle, before the asterik.

Thoughts anyone?
I wouldn't expect it to work with the change, but what was the incompatibility?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Posting a comment so you know that I haven't fallen off the face of Earth.  I don't want to just accept a comment as an answer in order to close this, and I don't want to give up yet.  I just haven't had time to get back to this, nor will I for awhile.  If another two weeks pass then I will close the question.
Thanks.