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.
LVL 6
KingDumbNoAsked:
Who is Participating?
 
DominicCroninCommented:
Adding the dot won't particularly help you in most regex languages. A rough translation would be

<keyword> followed by <zero or more characters that aren't an open-brace [1] (non-greedily)> followed by <keyword> follewed by <zero or more of anything [2] (non-greedily)> followed by <open-brace>

In your second variation, adding the dot means that the *? applies to your dot, and not the character class [^\}] which was what you wanted.

[1] Depending on your regex language, you might also be matching backslashes!
[2] The meaning of the dot is different in different regex languages. In some languages a dot matches new lines, and not in others, just to give one important example.

What does the numega docuementation say about the regex language they use?
0
 
jhshuklaCommented:
grep -e "(for|while|if|else|do).*?(for|while|if|else|do)\{" filename
0
 
jhshuklaCommented:
correction: grep -e "(for|while|if|else|do)[^\{]*?(for|while|if|else|do).*?\{" filename
google for perl regex. i believe that grep has approximately the same regex definitions as perl. don't know how to search across lines. :(
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
ozoCommented:
to search across lines:
perl -n0777 -e 'print /(\b(?:for|while|if|else|do)\b[^{]*?\b(?:for|while|if|else|do)\b)/g' filename
0
 
DominicCroninCommented:
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
0
 
KingDumbNoAuthor Commented:
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
0
 
jhshuklaCommented:
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
0
 
KingDumbNoAuthor Commented:
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?
0
 
ozoCommented:
I wouldn't expect it to work with the change, but what was the incompatibility?
0
 
KingDumbNoAuthor Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.