GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||
GeezerVison is showing blog.ostermiller.org — Finding Comments in Source Code Using Regular Expressions - Stephen Ostermiller Many text editors have advanced find (and replace) features. When I'm programming, I like to use an editor with regular expression search and replace. This feature is allows one to find text based on complex patterns rather than based just on literals. Upon occasion I want to examine each of the comments in my source code and either edit them or remove them. I found that it was difficult to write a regular expression that would find C style comments (the comments that start with /* and end with */) because my text editor does not implement the "non-greedy matching" feature of regular expressions.
First TryWhen first attempting this problem, most people consider the regular expression:
This seems the natural way to do it. The first problem with this approach is that /* First comment first comment—line two*/ /* Second comment */ Second TryThis can be overcome easily by replacing the
This reveals a second, more serious, problem—the expression matches too much. Regular expressions are greedy, they take in as much as they can. Consider the case in which your file has two comments. This regular expression will match them both along with anything in between: start_code();
/* First comment */
more_code();
/* Second comment */
end_code();
Third TryTo fix this, the regular expression must accept less. We cannot accept just any character with a
This simplistic approach doesn't accept any comments with a /* * Common multi-line comment style. */ /* Second comment */ Fourth TryThis is where it gets tricky. How do we accept a
This works better but again accepts too much in some cases. It will accept any even number of start_code();
/****
* Common multi-line comment style.
****/
more_code();
/*
* Another common multi-line comment style.
*/
end_code();
Fifth TryWhat we tried before will work if we accept any number of
Now the regular expression does not accept enough again. Its working better than ever, but it still leaves one case. It does not accept comments that end in multiple /**** * Common multi-line comment style. ****/ /**** * Another common multi-line comment style. */ SolutionNow we just need to modify the comment end to allow any number of
We now have a regular expression that we can paste into text editors that support regular expressions. Finding our comments is a matter of pressing the find button. You might be able to simplify this expression somewhat for your particular editor. For example, in some regular expression implementations, This is easy to augment so that it will also find
An Easier MethodNon-greedy MatchingMost regular expression packages support non-greedy matching. This means that the pattern will only be matched if there is no other choice. We can modify our second try to use the non-greedy matcher
CaveatsComments Inside Other ElementsAlthough our regular expression describes c-style comments very well, there are still problems when something someString = "An example comment: /* example */"; // The comment around this code has been commented out. // /* some_code(); // */ The solution to this is to write regular expressions that describe each of the possible larger elements, find these as well, decide what type of element each is, and discard the ones that are not comments. There are tools called lexers or tokenizers that can help with this task. A lexer accepts regular expressions as input, scans a stream, picks out tokens that match the regular expressions, and classifies the token based on which expression it matched. The greedy property of regular expressions is used to ensure the longest match. Although writing a full lexer for C is beyond the scope of this document, those interested should look at lexer generators such as Flex and JFlex. | ||||||||||||||||||||||||||||||
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |