SPUG: regular expression question

jerry gay jerry.gay at gmail.com
Tue Nov 15 05:57:00 PST 2005


On 11/14/05, Charles Connolly <cconnoll at u.washington.edu> wrote:
> Hi,
>      I'm having some trouble with a regular expression, and I thought I'd toss
> it out to the list, especially since it seems quiet here lately.
>      I'd like to process a file line by line, capturing the everything from the
> start of the line to a hash mark (#), which would indicate a comment. Here are
> some things I tried:
>
> All suggestions are welcomed.
>
use the CPAN. from the Regexp::Common docs:

    use Regexp::Common qw /comment/;

    while (<>) {
        /$RE{comment}{C}/       and  print "Contains a C comment\n";
        /$RE{comment}{C++}/     and  print "Contains a C++ comment\n";
        /$RE{comment}{PHP}/     and  print "Contains a PHP comment\n";
        /$RE{comment}{Java}/    and  print "Contains a Java comment\n";
        /$RE{comment}{Perl}/    and  print "Contains a Perl comment\n";
        /$RE{comment}{awk}/     and  print "Contains an awk comment\n";
        /$RE{comment}{HTML}/    and  print "Contains an HTML comment\n";
    }

as you can see from the examples in previous emails, parsing comments
can be tricky. if you're working with files that use a comment style
that matches any of the above listed programming languages, i think
your best bet is to use the widely-used, well-tested Regexp::Common
module. if you still want to write your own for a learning experience,
i suggest you copy the appropriate test files from the distribution
and use them as your guide.

~jerry


More information about the spug-list mailing list