| 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
|