Solved

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

Posted on 2004-10-05
10
267 Views
Last Modified: 2010-05-19
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.
0
Comment
Question by:KingDumbNo
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 9

Expert Comment

by:jhshukla
Comment Utility
grep -e "(for|while|if|else|do).*?(for|while|if|else|do)\{" filename
0
 
LVL 9

Assisted Solution

by:jhshukla
jhshukla earned 200 total points
Comment Utility
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
 
LVL 84

Assisted Solution

by:ozo
ozo earned 100 total points
Comment Utility
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
 
LVL 6

Expert Comment

by:DominicCronin
Comment Utility
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
 
LVL 6

Author Comment

by:KingDumbNo
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 9

Expert Comment

by:jhshukla
Comment Utility
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
 
LVL 6

Author Comment

by:KingDumbNo
Comment Utility
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
 
LVL 84

Expert Comment

by:ozo
Comment Utility
I wouldn't expect it to work with the change, but what was the incompatibility?
0
 
LVL 6

Accepted Solution

by:
DominicCronin earned 200 total points
Comment Utility
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
 
LVL 6

Author Comment

by:KingDumbNo
Comment Utility
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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now