Solved

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

Posted on 2004-10-05
10
271 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb example issues 3 88
How come this regular expression allows "e"? 14 66
programming a polycom voip phone 3 84
Java pass by reference 3 73
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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.
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

739 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