For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > October 2004 > Re: m/(\/\*[.|\n]*\*\/)/ to try and match C-Style multiline comments.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: m/(\/\*[.|\n]*\*\/)/ to try and match C-Style multiline comments.
Paul Lalli

2004-10-18, 4:00 pm

Vijai Kalyan wrote:
> I am learning perl so was working my way through the Camel book's
> first chapter. I wrote the following program to match all C-style
> multiline comments.


<snip>

> ---------------
>
> For RE: m/(\/\*.*?\*\/)/
>
> I got matches for multi-line comments containing no \n. For example:
>
> /* ... */
>
> but not
>
> /*
> */
>
> I looked up "." and found that . won't match a \n. So, I modified the
> RE to
>
> m/(\/\*[.|\n]*\*\/)/
>
> but this didn't help either. If I got it right


Obviously, you didn't get it right. You're using a character class
which matches an actual period, vertical-bar, or a newline. Keep
reading the documentation in your Camel or in
perldoc perlretut
perldoc perlre

The correct way to get the period to match a newline is to add the /s
modifier onto your regexp.

> Any suggestions?


1) Keep reading the documentation. (That's not a criticism, it's a
compliment - too many questioners in this group don't read and just
expect everyone to read the docs to them). Specifically, you need to
read up on character classes and regexp modifiers.
2) if you're doing this as a learning exercise, make . match the newline
the correct way.
3) If you're doing this for production code, don't bother rolling your
own solution - check the Perl FAQ before doing something like this:
perldoc -q comment

Paul Lalli
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com