Solved

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

Posted on 2004-10-05
10
269 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
ID: 12234239
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
ID: 12234259
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
ID: 12235131
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 6

Expert Comment

by:DominicCronin
ID: 12240578
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
ID: 12240999
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
 
LVL 9

Expert Comment

by:jhshukla
ID: 12246087
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
ID: 12292459
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
ID: 12303120
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
ID: 12310661
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
ID: 12465405
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 98
firstswap challenge 20 79
Query group by data in SQL Server - cursor? 3 35
Identify two keyboard codes and how to enter them 9 30
A short article about a problem I had getting the GPS LocationListener working.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

825 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